Question

I create a closed region on a picture by impoly('Closed', true) and lastly after marking the area for the mask BW = createMask(h) in Matlab commandline. Initial commands before marking points for the mask in the figure

imshow('contour.png');
h = impoly('Closed',true);

Here, I used nkjt's answer below. The picture to be filtered by the function conditionalRemoval(image, area)

enter image description here

Then, I run

image = imread('contour.png');
areaLazyRemoval = BW; 
image = conditional_removal(image, areaLazyRemoval);

I now have the mask and the picture. I should apply the function conditional_removal to them.

How can you use this mask now and apply the function to it in the its marked region?

My function conditional_removal's pseudocode is

function [ image ] = conditional_removal( image, areaLazyRemoval )
   % dim image 794 x 1001 x 3 uint
   % dim areaLazyRemoval 794 x 1001 logical
   image(:,:,1) .* areaLazyRemoval;    % TODO wrong operator here!
   % all pixels marked by logical ones in areaLazyRemoval should get lazyRemoval applied
   % else greedyRemoval so zero
   %
end
%%%%%%%%%%%%%%%%%%%%%%%
% lazy removal function
% so remove by 50% chance the thing
function pixel = lazyRemoval(pixel)
if randn > 0
    pixel = 0;
end 
% TODO how to apply pixel-wise removal to the logical matrix and image?

How can you apply the pixel-wise removal to the image by the logical matrix, mask?

Was it helpful?

Solution

By this:

impoly > Generate Data > function createfigure1

Do you mean after calling impoly, you go into the figure window and select "Generate Code"? Which will create a function createfigure - but this has nothing to do with impoly.

There are a couple of ways you can extract the ROI.
After choosing the area with impoly, before closing the figure:

BW = createMask(h);

Or you can extract the position with getPosition and then use roipoly.

OTHER TIPS

The last problems were in the function conditional_removal and in creating the randn_logical_matrix.

Matlab docs of randn says

RN = distributed.randn(..., classname) specifies the class of the distributed array D. Valid choices are the same as for the regular randn function: 'double' (the default), 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', and 'uint64'.

So this suggests me that it is not supported directly by the existing functions.

So my attempt to make the this logical random number generator and applying the function through a mask

clear;
close all;
image = imread('contour.png');
load('mask_1.mat');
areaLazyRemoval = BW;
image = image(:,:,1);
image  = conditional_removal(image, areaLazyRemoval);
image = double(image);
contour(image);
colorbar;
caxis([-2,1.5].*10^7);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function matrix = make_random_logical_matrix(matrix)

[row_max,column_max] = size(matrix);
matrix = randn(row_max,column_max);
for row = 1:row_max
    for column = 1:column_max
        t = randn;
        if t >= 0
            matrix(row,column) = 0;
        end
        if t < 0
            matrix(row,column) = 1;
        end
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [ image ] = conditional_removal( image, areaLazyRemoval )
    random_logical_matrix = make_random_logical_matrix(areaLazyRemoval);
    areaLazyRemoval = areaLazyRemoval .* random_logical_matrix;
    areaLazyRemoval = uint8(areaLazyRemoval);
    image = image(:,:,1) .* areaLazyRemoval;
end

and the contour

enter image description here

and the mesh

enter image description here

which seems to be ok solution. To improve, I think the norm of the situation should be calculated and then the conditional_removal algorithm improved accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top