필터 지우기
필터 지우기

get the actual position correspond to imfreehand

조회 수: 1 (최근 30일)
chess
chess 2014년 8월 17일
편집: chess 2014년 8월 22일
Hi I have a question. I have a grayscale image, I chose a region using imfreehand command and I have its position. I can convert to the mask but that is a binary image how can I extract the exact image that is correspond to region of imfreehand from the grayscale image. Meaning that imcrop returns the cropped version of grayscale image I want to do the same thing here.
imshow(image1)
h=imfreehand;
pos=getPosition(h)
Then I like to have something like image(pos) to extract the portion from grayscale image. However this is of course is a error because the subscript is a fraction.

채택된 답변

Ben11
Ben11 2014년 8월 17일
After you get the position, you can create a mask in which value inside the ROi are equal to 1 and those outside are equal to 0. Then you assign those 0-values to the original image. Please try the following code and see what happens, is this what you want to achieve?
clear
clc
A = rgb2gray(imread('peppers.png'));
figure;
hold on
subplot(1,2,1)
imshow(A)
hRoi = imfreehand(gca);
Position = getPosition(hRoi);
BW = createMask(hRoi);
A(BW == 0) = 0;
subplot(1,2,2)
imshow(A)
hold off
  댓글 수: 2
chess
chess 2014년 8월 17일
That is good but how can I only get the portion I like meaning that zero part should be removed. How to resize the image to only have what I want like imcrop.
Ben11
Ben11 2014년 8월 17일
I think the best you can do is crop a rectangular image with the boundaries of the ROI touching the sides, is it what you mean? If so you can you imcrop with the coordinates of the enclosing black rectangle? For example, "Position" in my example above outputs a 2-column array, in which the first corresponds to the x and the 2nd corresponds to the y-coordinates. You can fetch the maximum and minimum for each coordinate:
xmin = min(Position(:,1))
xmax = max(Position(:,1))
ymin = min(Position(:,2))
ymax = max(Position(:,2))
and then use imcrop as usual.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 8월 20일
See my freehand demos attached.

카테고리

Help CenterFile Exchange에서 Build Interactive Tools에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by