Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Crop an Image without changing it to Binary

Subject: Crop an Image without changing it to Binary

From: Nehal

Date: 10 Jul, 2010 09:13:04

Message: 1 of 11

I need to crop an object without changing the image into binary. I cannot crop the object manually by putting positions/co-ordinates.

In my case, the object is a hand. Background is black and the hand is white. I need to crop out the hand without converting the image to binary.

The RGB image is here: http://www.mediafire.com/?znwngzjydn3

Can anyone help me..?

Subject: Crop an Image without changing it to Binary

From: Image Analyst

Date: 10 Jul, 2010 11:27:05

Message: 2 of 11

Find the edges of the hand somehow, for example when the summed profiles pass some threshold.
verticalProfile = mean(imageArray, 2);
horizontalProfile = mean(imageArray, 1);

row1 = find(verticalProfile > thresholdvalue, 1, 'first');
row2 = find(verticalProfile > thresholdvalue, 1, 'last');

column1 = find(horizontalProfile > thresholdvalue, 1, 'first');
column2 = find(horizontalProfile > thresholdvalue, 1, 'last');

You have to determine thresholdvalue of course.
Then call imcrop().

width = abs(column2-column1)+1;
height = abs(row2-row1)+1;
croppedImage = imcrop(rgbImage, [row1 column1 width height]);

Subject: Crop an Image without changing it to Binary

From: Nehal

Date: 10 Jul, 2010 13:50:20

Message: 3 of 11

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i19le9$kgv$1@fred.mathworks.com>...
> Find the edges of the hand somehow, for example when the summed profiles pass some threshold.
> verticalProfile = mean(imageArray, 2);
> horizontalProfile = mean(imageArray, 1);
>
> row1 = find(verticalProfile > thresholdvalue, 1, 'first');
> row2 = find(verticalProfile > thresholdvalue, 1, 'last');
>
> column1 = find(horizontalProfile > thresholdvalue, 1, 'first');
> column2 = find(horizontalProfile > thresholdvalue, 1, 'last');
>
> You have to determine thresholdvalue of course.
> Then call imcrop().
>
> width = abs(column2-column1)+1;
> height = abs(row2-row1)+1;
> croppedImage = imcrop(rgbImage, [row1 column1 width height]);

Can you suggest some thresholdvalues...? I have tried some values like 0.5, 0.7 ect. But it's not working. Can you suggest me a range of values that I should try...?

and...
> croppedImage = imcrop(rgbImage, [row1 column1 width height]);
should it be the RGB image or the gray image...?

Thanks.

Subject: Crop an Image without changing it to Binary

From: Image Analyst

Date: 10 Jul, 2010 14:49:05

Message: 4 of 11

Without seeing an image so I can look at it's histogram, you want me to suggest a threshold value?

Maybe you can try the triangle threshold method. It works well for many real world situations:
http://www.mathworks.com/matlabcentral/fileexchange/28047-gray-image-thresholding-using-the-triangle-method

Otherwise, just import your image into a more interactive program, like ImageJ or Photoshop and see what threshold works.

Subject: Crop an Image without changing it to Binary

From: Walter Roberson

Date: 10 Jul, 2010 15:54:33

Message: 5 of 11

Nehal wrote:
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message
> <i19le9$kgv$1@fred.mathworks.com>...
>> Find the edges of the hand somehow, for example when the summed
>> profiles pass some threshold. verticalProfile = mean(imageArray, 2);
>> horizontalProfile = mean(imageArray, 1);
>>
>> row1 = find(verticalProfile > thresholdvalue, 1, 'first');
>> row2 = find(verticalProfile > thresholdvalue, 1, 'last');

> Can you suggest some thresholdvalues...?

Not germaine, as ImageAnalyst's method implicitly converts the image to
binary ('>' returns a binary result) and thus does not satisfy your
requirement that the image must not be converted to binary. Not that I
can think of a good reason why you would impose such a requirement as
long as your _output_ was full color, but I guess you have your reasons.

Subject: Crop an Image without changing it to Binary

From: Image Analyst

Date: 10 Jul, 2010 16:33:04

Message: 6 of 11

I believe he's wanting a cropped masked image, or a cropped gray image. In another post he gave the URL of his grayscale hand image, and some binary version of it. Now I think he wants either the grayscale image cropped out of the original, or a masked version of the grayscale image cropped out of the original. Going to binary is the first step (or could be), using whatever method works. Then you can find the bounding box (in code that I gave in his prior post). Then you can get a masked image by multiplying the binary image by the grayscale image:
maskedImage = grayImage .* single(binaryImage);
Then you can crop out the bounding box from the masked image
croppedMaskedImage = imcrop(maskedImage, [row, col, width height]);
You can also crop out the orginal (unmasked) grayscale portion if desired:
croppedGrayImage = imcrop(grayImage, [row, col, width height]);

You can also find the bounding box by summing or taking the mean of the gray image in the two directions and seeing where the intensity crosses some threshold. (Essentially, seeing there a values falls below or exceeds some threshold value is pretty much the same as binarizing the profile of the image.)

If I've got it wrong, and you want some third option, then mock up something in Photoshop and post it.

Subject: Crop an Image without changing it to Binary

From: Nehal

Date: 10 Jul, 2010 19:36:15

Message: 7 of 11

Walter Roberson <roberson@hushmail.com> wrote in message <ZI0_n.35320$YX3.13077@newsfe18.iad>...
> Nehal wrote:
> > "Image Analyst" <imageanalyst@mailinator.com> wrote in message
> > <i19le9$kgv$1@fred.mathworks.com>...
> >> Find the edges of the hand somehow, for example when the summed
> >> profiles pass some threshold. verticalProfile = mean(imageArray, 2);
> >> horizontalProfile = mean(imageArray, 1);
> >>
> >> row1 = find(verticalProfile > thresholdvalue, 1, 'first');
> >> row2 = find(verticalProfile > thresholdvalue, 1, 'last');
>
> > Can you suggest some thresholdvalues...?
>
> Not germaine, as ImageAnalyst's method implicitly converts the image to
> binary ('>' returns a binary result) and thus does not satisfy your
> requirement that the image must not be converted to binary. Not that I
> can think of a good reason why you would impose such a requirement as
> long as your _output_ was full color, but I guess you have your reasons.

yes.. actually i have converted my the image binary then cropped the object (in my case: hand) with the help of Image Analyst... then i had to do some more operations on that cropped image... finally... the hand was not recognized anymore... somehow i came to an idea that if i could crop the hand out first and then make it binary and then if i do my processing steps... the hand will be recognized... that's why i'm trying this...

Subject: Crop an Image without changing it to Binary

From: Nehal

Date: 10 Jul, 2010 19:40:21

Message: 8 of 11

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i1a7c0$aih$1@fred.mathworks.com>...
> I believe he's wanting a cropped masked image, or a cropped gray image. In another post he gave the URL of his grayscale hand image, and some binary version of it. Now I think he wants either the grayscale image cropped out of the original, or a masked version of the grayscale image cropped out of the original. Going to binary is the first step (or could be), using whatever method works. Then you can find the bounding box (in code that I gave in his prior post). Then you can get a masked image by multiplying the binary image by the grayscale image:
> maskedImage = grayImage .* single(binaryImage);
> Then you can crop out the bounding box from the masked image
> croppedMaskedImage = imcrop(maskedImage, [row, col, width height]);
> You can also crop out the orginal (unmasked) grayscale portion if desired:
> croppedGrayImage = imcrop(grayImage, [row, col, width height]);
>
> You can also find the bounding box by summing or taking the mean of the gray image in the two directions and seeing where the intensity crosses some threshold. (Essentially, seeing there a values falls below or exceeds some threshold value is pretty much the same as binarizing the profile of the image.)
>
> If I've got it wrong, and you want some third option, then mock up something in Photoshop and post it.

I do not think i am trying to crop a mask image... because mask will create some shades in the image.... i just want to crop the hand from the image i have posted in my first post here...

and could you plz explain me how the "thresholdvalue" works here...? because I have managed to find the threshold value of the grayimage... bt i am confused how this works...?

Subject: Crop an Image without changing it to Binary

From: Image Analyst

Date: 10 Jul, 2010 21:48:05

Message: 9 of 11

"Nehal " <arnab620@yahoo.com> wrote in message <i1aib5$4ra$1@fred.mathworks.com>...
> and could you plz explain me how the "thresholdvalue" works here...? because I have managed to find the threshold value of the grayimage... bt i am confused how this works...?

I'm not sure what you mean (again). You said you have managed to find the threshold. So you just threshold like this
binaryImage = grayImage > thresholdValue; % or you can use <
Then where the image is brighter than the threshold it will be true (1) and where it's less it will be false (0). What about this is confusing you?

By the way, it doesn't make any difference if you crop and then binarize or binarize then crop, as long as the threshold value is the same.
-ImageAnalyst

Subject: Crop an Image without changing it to Binary

From: Nehal

Date: 15 Jul, 2010 18:31:23

Message: 10 of 11

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <i1apqk$t9a$1@fred.mathworks.com>...
> "Nehal " <arnab620@yahoo.com> wrote in message <i1aib5$4ra$1@fred.mathworks.com>...
> > and could you plz explain me how the "thresholdvalue" works here...? because I have managed to find the threshold value of the grayimage... bt i am confused how this works...?
>
> By the way, it doesn't make any difference if you crop and then binarize or binarize then crop, as long as the threshold value is the same.
> -ImageAnalyst

yes.. it really doesn't make any difference... I was just trying it through a different way... nothing else...

finally I have managed to do my work... I was not able to crop the gray image though... I converted the the RGB image to binary using a threshold value... it did work... bt I am not sure how it worked...

could your plz make me clear one thing...? what is the difference between:

binaryImage = im2bw(I, graythresh(I)); % I is the RGB Image array
and
binaryImage = grayImage == 255; % grayImage is the GRAY image of I

Thanks.

Subject: Crop an Image without changing it to Binary

From: Faisal Faqir

Date: 19 Jun, 2012 20:54:08

Message: 11 of 11

"Image Analyst" wrote in message <i19le9$kgv$1@fred.mathworks.com>...
> Find the edges of the hand somehow, for example when the summed profiles pass some threshold.
> verticalProfile = mean(imageArray, 2);
> horizontalProfile = mean(imageArray, 1);
>
> row1 = find(verticalProfile > thresholdvalue, 1, 'first');
> row2 = find(verticalProfile > thresholdvalue, 1, 'last');
>
> column1 = find(horizontalProfile > thresholdvalue, 1, 'first');
> column2 = find(horizontalProfile > thresholdvalue, 1, 'last');
>
> You have to determine thresholdvalue of course.
> Then call imcrop().
>
> width = abs(column2-column1)+1;
> height = abs(row2-row1)+1;
> croppedImage = imcrop(rgbImage, [row1 column1 width height]);

Hello, In my case I want to find the edges of the face to minimise my ROI for eye detection, for verticalProfile = mean(imageArray, 2); i didn't understand imageArray is it number of images stored in an array or just a matrix represnting the image?? Thank you I will be waiting for the reply : )

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us