0

For instance, I start my program as follows reading some image:

I=input('image name: ','s');
img=double(imread(I));

I'm planning to work only on some portion of that image. So, I noticed that I may need h=imfreehand for this purpose.

Thus, I have inserted h=imfreehand under the two lines above. What I got is a white screen. So, how can I get the image and select the region I want? The other thing is, how can I tell my program to work only on that region I have selected?

Thanks.

UPDATE

I did the following in a portion of my code:

figure, imshow(img);
h = imfreehand;
position = wait(h);

% post processing
se=strel('disk',3);
erosion=imerode(h,se);
result_image=imsubtract(h,erosion);.

But, I got the following error:

Error using imerode
Expected input number 1, IM, to be one of these types:

numeric, logical

Instead its type was imfreehand.

Error in morphop>CheckInputImage (line 335)
validateattributes(A, {'numeric' 'logical'}, {'real' 'nonsparse'}, ...

Error in morphop>ParseInputs (line 166)
A = CheckInputImage(A, func_name);

Error in morphop (line 14)
[A,se,pre_pad,...

Error in imerode (line 123)
B = morphop(A,se,'erode',mfilename,varargin{:});

Error in program (line 155)
erosion=imerode(h,se);

Is it to do with erosion? What can be done in this case?

`

Simplicity
  • 47,404
  • 98
  • 256
  • 385

3 Answers3

4

Following the advice in the matlab documentation try this:

I=input('image name: ','s');
img=imread(I);
figure, imshow(img);
h = imfreehand;
position = wait(h); 

Edit:

The documentation suggests as an alternative

figure, imshow(img);
h = imfreehand(gca);
Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
  • 1
    Thanks for your reply. Having tried your solution, I got the following error: `Error using var (line 59) First argument must be single or double. Error in std (line 32) y = sqrt(var(varargin{:})); Error in @(x)std(x(:)) Error in nlfilter (line 61) b = mkconstarray(class(feval(fun,aa(1+rows,1+cols),params{:})), 0, size(a)); Error in program (line 38) standard_deviation_matrix=nlfilter(img,[5 5],fun_standard_deviation);` – Simplicity Aug 01 '13 at 12:19
  • 1
    When I apply my rest of the code, does it apply the operations on the region that I selected by default, or I have to specify what to work with to the program? Thanks – Simplicity Aug 01 '13 at 12:23
  • 1
    You apply operations on the handle `h` to the object created by imfreehand. I would check the methods that you can use with the freehand object generated by imfreehand, listed in the documentation. You manipulate the object using its handle h, for instance you can create a mask with `BW = createMask(h)` – Buck Thorn Aug 01 '13 at 12:31
2

Try passing the handle of the plot to imfreehand()

I=input('image name: ','s');
img=double(imread(I));

figure;
ih=image(img);
h=imfreehand(ih)

Sorry, I don't have the image processing toolbox to test this.

mor22
  • 1,372
  • 1
  • 15
  • 32
  • 2
    Almost an exact clone, beat by 19 sec! – Buck Thorn Aug 01 '13 at 11:52
  • 1
    Try not. Do... or do not. – Buck Thorn Aug 01 '13 at 11:55
  • 1
    @mor22. Thanks for your reply. I got this error when tried your solution: `Error using imfreehand>imfreehandAPI (line 156) HPARENT must be able to have an hggroup object as a child. Error in imfreehand (line 83) [h_group,draw_api] = imfreehandAPI(varargin{:}); Error in program (line 8) h=imfreehand(ih);` – Simplicity Aug 01 '13 at 12:08
  • 1
    Sorry I can't test this 'cause I don't have the improcessing toolbox. checkout http://www.mathworks.co.uk/help/matlab/ref/hggroup.html – mor22 Aug 01 '13 at 12:10
2

Seems like converting your image (possibly uint8) to double causes a problem.

I would either do:

  • use the original coding of the image (for example img_uint8 = imread('coins.png') is coded using integers). The problem is that you won't probably be able to use imfreehand as it needs to read double or single precision float.
  • convert the image using img_double = double(imread('coins.png')) so that imfreehand will work. The conversion however causes a display problem that you can bypass with imshow(img_double,[]) that is an analogue of imshow(img_double, [min(min(img_double)), max(max(img_double))]). It forces imshow to correctly use the full range of data (255 Vs. 255.0)

The best option is therefore the second one.

To use imfreehand, @Try Hard gave a nice code h = imfreehand or h = imfreehand(gca)

marsei
  • 7,691
  • 3
  • 32
  • 41