Thread Subject: How to crop an image from pixel scan?

Subject: How to crop an image from pixel scan?

From: wilson

Date: 28 Oct, 2009 20:13:03

Message: 1 of 19

I had use the for loop to scan the images pixel,

How am i gonna to store the 1st point (x value,y value)of the data, in order to compare to the second point to get the length for cropping?

Thanks

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 28 Oct, 2009 23:26:44

Message: 2 of 19

On Oct 28, 4:13 pm, "wilson " <wilson_silver3...@hotmail.com> wrote:
> I had use the for loop to scan the images pixel,
>
> How am i gonna to store the 1st point (x value,y value)of the data, in order to compare to the second point to get the length for cropping?
>
> Thanks

------------------------------------------------------
The first point is already stored in your image array. If you want to
save it out to its own special separate variable, you just do
something like
firstPoint = imageArray(1,1);
Now, where did you post your image? What's its URL?

Subject: How to crop an image from pixel scan?

From: wilson

Date: 29 Oct, 2009 13:58:03

Message: 3 of 19

i take this image as example: http://algo.inria.fr/chyzak/face.jpg
now i wanna to crop his eyes, i had done the segmentation and converted the colour to grayscale, how am i gonna crop the iris from the image?

a=imread('face.jpg');
b=rgb2gray(a);
c=imcrop(b)
imtool(c);
[x y] = size (c);
 
 for i = 1:59
     for j = 1:177
     
     if (c(i,j) > 150 )
         
     c(i,j) = 255;
else
c(i,j)=0;
end
end
end
imshow(c);

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 29 Oct, 2009 15:22:07

Message: 4 of 19

I'm not sure what the "thresholding" stuff in your code is supposed to
do. But anyway, I sort of cleaned it up a little bit. Try this:

clc;
clear all;
close all;
workspace;
% Read in the image. 'peppers.png' is a standard MATLAB demo image.
% You can replace it with your face.jpg image.
originalImage = imread('peppers.png');
subplot(2,2,1);
imshow(originalImage);
set(gcf, 'Position', get(0, 'ScreenSize')); % Maximize figure.
title('Original Color Image');

% Convert to monochrome
monoImage = rgb2gray(originalImage);
subplot(2,2,2);
imshow(monoImage);
title('Monochrome Image');

% Ask user to crop the image.
uiwait(msgbox('Please crop the monochrome image'));
croppedImage = imcrop(monoImage);
title('Monochrome Image'); % Put it back - it vanished for some
reason.
subplot(2,2,3);
imshow(croppedImage);
title('Cropped Image');

% Segment the image according to Wilson's (arbitrary) criteria.
segmentedImage = croppedImage;
segmentedImage(croppedImage>150) = 255;
subplot(2,2,4);
imshow(segmentedImage);
title('Segmented Image');

Subject: How to crop an image from pixel scan?

From: wilson

Date: 30 Oct, 2009 00:29:03

Message: 5 of 19

Thanks ImageAnalyst your info do really help me a lots!

If lets said i using template matching method will i able to crop the image out?

>>im1=imread('face.jpg');
>> im2=imread('eye.bmp');

%tmp is the templete matching using power of the image

>> result1=tmp(im1,im2);
>>result1=tmp(im1,im2);
>>figure,
>>subplot(2,2,1),imshow(im1);title('Template');
>>subplot(2,2,2),imshow(im2);title('Target');
>>subplot(2,2,3),imshow(result1);title('Matching Result using tmp');

i get this coding from the template matching file exchange there! So i goona ask u which is the suitable ways to crop an eye from an image automatically? Can u gv me some advice on that because i still a beginner in Matlab there still many thing i need to learn from u guys!
Thanks cheers

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 30 Oct, 2009 01:07:01

Message: 6 of 19

I don't know what that routine does, and I don't have time to check it
out. But bottom line: if it does what you want, great. If it
doesn't, then program up something yourself.

Subject: How to crop an image from pixel scan?

From: wilson

Date: 30 Oct, 2009 01:16:04

Message: 7 of 19

its ok ImageAnalyst cheers u do help me a lots thanks!
I will try to figure it out how am i going to crop my image automatically!

Subject: How to crop an image from pixel scan?

From: bulitpruf

Date: 9 Nov, 2009 21:59:25

Message: 8 of 19

Hey this is interesting stuff I am new to matlab and I wish to learn
how to create an aligorithm to crop scanned geometrical images, like a
cricle, rectangle, triangle and square. All these solid should be
drawn on different parts of the paper and at varied sizes. So as the
algorithm will not look as if it is cropping the solid at one
predetermine size and location on the page.

Subject: How to crop an image from pixel scan?

From: wilson

Date: 9 Nov, 2009 22:24:02

Message: 9 of 19


I still cant figure it out how am i gonna do it yet! Now i only got the concept of doing it but i dunno how to write the coding on it! I already do this research for 1 month but i still cant manage to write the coding out! Perhaps i really need some MATLAB pro to teach me on the image processing in matlab. Now i just manage to change the image to binary number. And i cant find a way out on how am i gonna crop the eye from the face by using pixel scan! Sorry bro i dunno how am i gonna help u because i also a beginner in MATLAB

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 10 Nov, 2009 01:34:16

Message: 10 of 19

On Nov 9, 4:59 pm, bulitpruf <bpgu...@gmail.com> wrote:
> Hey this is interesting stuff I am new to matlab and I wish to learn
> how to create an aligorithm to crop scanned geometrical images, like a
> cricle, rectangle, triangle and square. All these solid should be
> drawn on different parts of the paper and at varied sizes. So as the
> algorithm will not look as if it is cropping the solid at one
> predetermine size and location on the page.

--------------------------------------------------------------------------------------------------------------------
bulitpruf:
Not too hard. It might seem complicated at first, but just take it
one step at a time, like in this demo:
(After you paste in to MATLAB, you'll probably have to fix a lot of
lines that get broken due to the newsgroup software. Just fix those
and you'll be ready to go.)
It's just a slightly modified version of my demo at
http://www.mathworks.com/matlabcentral/fileexchange/25157
This one will find the 6 shapes and show them in the first figure
window, and then bring up a second window where it extracts out each
shape's bounding box and displays it in it's own axes in the figure
window.

% Startup code.
tic; % Start timer.
clc; % Clear command window.
clear all; % Get rid of variables from prior run of this m-file.
close all; % close all open figure windows.
disp('Running BlobsDemo.m...'); % Message sent to command window.
workspace; % Show panel with all the variables.

% Read in standard MATLAB demo image
originalImage = imread('pillsetc.png');
originalImage = rgb2gray(originalImage);
subplot(2, 3, 1); imagesc(originalImage); colormap(gray(256));
caption = sprintf('Original "coins" image showing\n6 nickels (the
larger coins) and 4 dimes (the smaller coins).');
title(caption);
axis square; % Make sure image is not artificially stretched because
of screen's aspect ratio.
% Maximize the figure window.
set(gcf, 'Position', get(0, 'ScreenSize'));

% Just for fun, let's get its histogram.
[pixelCount grayLevels] = imhist(originalImage);
subplot(2, 3, 2);
bar(pixelCount); title('Histogram of original image');
xlim([0 grayLevels(end)]); % Scale x axis manually.

% Threshold the image to get a binary image (only 0's and 1's) of
class "logical."
% Method #1: using im2bw()
% normalizedThresholdValue = 0.4; % In range 0 to 1.
% thresholdValue = normalizedThresholdValue * max(max
(originalImage)); % Gray Levels.
% binaryImage = im2bw(originalImage,
normalizedThresholdValue); % One way to threshold to binary
% Method #2: using a logical operation.
  thresholdValue = 125;
  binaryImage = originalImage > thresholdValue; % Bright objects will
be the chosen if you use >.
% binaryImage = originalImage < thresholdValue; % Dark objects will
be the chosen if you use <.

% Do a "hole fill" to get rid of any background pixels inside the
blobs.
binaryImage = imfill(binaryImage, 'holes');

% Show the threshold as a vertical red bar on the histogram.
hold on;
maxYValue = ylim;
hStemLines = stem(thresholdValue, maxYValue(2), 'r');
children = get(hStemLines, 'children');
set(children(2),'visible', 'off');
% Place a text label on the bar chart showing the threshold.
annotationText = sprintf('Thresholded at %d gray levels',
thresholdValue);
% For text(), the x and y need to be of the data class "double" so
let's cast both to double.
text(double(thresholdValue + 5), double(0.5 * maxYValue(2)),
annotationText, 'FontSize', 10, 'Color', [0 .5 0]);
text(double(thresholdValue - 70), double(0.94 * maxYValue(2)),
'Background', 'FontSize', 10, 'Color', [0 0 .5]);
text(double(thresholdValue + 50), double(0.94 * maxYValue(2)),
'Foreground', 'FontSize', 10, 'Color', [0 0 .5]);


% Display the binary image.
subplot(2, 3, 3); imagesc(binaryImage); colormap(gray(256)); title
('Binary Image, obtained by thresholding'); axis square;

% Get rid of blobs less than 800 pixels in area.
binaryImage = bwareaopen(binaryImage, 800);

labeledImage = bwlabel(binaryImage, 8); % Label each blob so we
can make measurements of it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); %
pseudo random color labels

subplot(2, 3, 4); imagesc(labeledImage); title('Labeled Image, from
bwlabel()'); axis square;
subplot(2, 3, 5); imagesc(coloredLabels); title('Pseudo colored
labels, from label2rgb()'); axis square;

% Get all the blob properties. Can only pass in originalImage in
version R2008a and later.
blobMeasurements = regionprops(labeledImage, originalImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);

% bwboundaries returns a cell array, where each cell
% contains the row/column coordinates for an object in the image.
% Plot the borders of all the coins on the original
% grayscale image using the coordinates returned by bwboundaries.
subplot(2, 3, 6); imagesc(originalImage); title('Outlines, from
bwboundaries()'); axis square;
hold on;
boundaries = bwboundaries(binaryImage);
for k = 1 : numberOfBlobs
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
hold off;

% Bring up another figure to hold the cropped portions.
figure;
set(gcf, 'Position', 0.75 * get(0, 'ScreenSize'));

fprintf(1,'Blob # Mean Intensity Area Perimeter Centroid
\n');
for k = 1 : numberOfBlobs % Loop through all blobs.
% Find the mean of each blob. (R2008a has a better way where you can
pass the original image
% directly into regionprops. The way below works for all versions
including earlier versions.)
    thisBlobsPixels = blobMeasurements(k).PixelIdxList; % Get list of
pixels in current blob.
    meanGL = mean(originalImage(thisBlobsPixels)); % Find mean
intensity (in original image!)
meanGL2008a = blobMeasurements(k).MeanIntensity; % Mean again, but
only for version >= R2008a

blobArea = blobMeasurements(k).Area; % Get area.
blobPerimeter = blobMeasurements(k).Perimeter; % Get perimeter.
blobCentroid = blobMeasurements(k).Centroid; % Get centroid.
    fprintf(1,'#%d %18.1f %11.1f %8.1f %8.1f %8.1f\n', k, meanGL,
blobArea, blobPerimeter, blobCentroid);

blobBoundingBox = blobMeasurements(k).BoundingBox; % Get centroid.
% Crop out this blob.
row1 = blobBoundingBox(2);
col1 = blobBoundingBox(1);
row2 = row1 + blobBoundingBox(4) - 1;
col2 = col1 + blobBoundingBox(3) - 1;
croppedImage = originalImage(row1:row2, col1:col2);
subplot(2,3,k);
imshow(croppedImage, []);
title(['Blob' num2str(k)]);
end

elapsedTime = toc;

Subject: How to crop an image from pixel scan?

From: bulitpruf

Date: 11 Nov, 2009 01:28:26

Message: 11 of 19

wow, wow wow
These are a few of the test images I wish to get crop, leaving only
the shape
http://gueze.tripod.com/smalltrianglenextposition.jpg
http://gueze.tripod.com/bigtriangle.jpg
http://gueze.tripod.com/circle.png
http://gueze.tripod.com/square.png

I tried to change the file name in the demo "'pillsetc.png'" to anyone
of these images and I am not getting them to crop
Wow you are good
Can you assist me again

Subject: How to crop an image from pixel scan?

From: bulitpruf

Date: 11 Nov, 2009 01:33:27

Message: 12 of 19


oh by the way I should also be able to take a photo of thest pictures
with a digital camera as well and algorithm should crop them
thanks again

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 11 Nov, 2009 02:32:33

Message: 13 of 19

On Nov 10, 8:28 pm, bulitpruf <bpgu...@gmail.com> wrote:
> wow, wow wow
> These are a few of the test images I wish to get crop, leaving only
> the shapehttp://gueze.tripod.com/smalltrianglenextposition.jpghttp://gueze.tripod.com/bigtriangle.jpghttp://gueze.tripod.com/circle.pnghttp://gueze.tripod.com/square.png
>
> I tried to change the file name in the demo "'pillsetc.png'" to anyone
> of these images and I am not getting them to crop
> Wow you are good
> Can you assist me again

-----------------------------------------------------------------
bulitpruf:
Well I do this stuff all day long and have been for 30 years.
I don't see why it shouldn't work. Did you read the comments?
Particularly the one here:

 thresholdValue = 125;
  binaryImage = originalImage > thresholdValue; % Bright objects will
be the chosen if you use >.
% binaryImage = originalImage < thresholdValue; % Dark objects will
be the chosen if you use <.

Since your objects are dark, you need something like
binaryImage = originalImage < thresholdValue; % Dark objects will be
chosen

Did you do that? Or did you ignore the comments and just let it keep
selecting bright objects despite the fact that your objects are dark?
If you did that, like the comments said, it should work.
Good luck,
ImageAnalyst

Subject: How to crop an image from pixel scan?

From: bulitpruf

Date: 11 Nov, 2009 21:32:28

Message: 14 of 19


>  thresholdValue = 125;
>   binaryImage = originalImage > thresholdValue; % Bright objects will
> be the chosen if you use >.
> %   binaryImage = originalImage < thresholdValue; % Dark objects will
> be the chosen if you use <.

man you not easy can I say

binaryImage = orginalImage < thresholdValue or binaryImage =
originalImage > thresholdValue

to search for images with both bright and dark objects

can I say that

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 11 Nov, 2009 21:38:45

Message: 15 of 19

Well I think it would be more like:

binaryImage = originalImage < darkThreshold & originalImage >
brightThreshold;

to get both the bright and dark objects in one single output image.

To get one of the other

binaryImageDarkObjects = originalImage < darkThreshold

binaryImageBrightObjects = originalImage > brightThreshold;

Of course you can AND them together to get both in a single image.

darkAndBrightObjects = bitand(binaryImageDarkObjects ,
binaryImageBrightObjects );
(basically what I did in the very first equation).

Subject: How to crop an image from pixel scan?

From: bulitpruf

Date: 15 Nov, 2009 03:12:02

Message: 16 of 19

thanks alot I will keep in touch

Subject: How to crop an image from pixel scan?

From: bulitpruf

Date: 19 Nov, 2009 02:51:19

Message: 17 of 19

sorry to bother you again as I said I am new to mathlab

just these two errors

??? Undefined function or variable 'darkThreshold'.

Error in ==> SACA at 38
   binaryImage = originalImage < darkThreshold & originalImage >
brightThreshold;

??? Undefined function or variable 'binaryImageDarkObjects'.

Error in ==> SACA at 39
   darkAndBrightObjects = bitand(binaryImageDarkObjects ,
binaryImageBrightObjects );

I really want both dark and bright images to be selected. but both
must work no matter what kind of image is read

Subject: How to crop an image from pixel scan?

From: Enigma Enlist

Date: 21 Mar, 2010 14:59:03

Message: 18 of 19

the user needs to crop the image himself a/c to the coding u suggested...if a user wants to crop half or three fourth of the original image automatically wt wud b the changing then?

Subject: How to crop an image from pixel scan?

From: ImageAnalyst

Date: 21 Mar, 2010 15:50:55

Message: 19 of 19

On Mar 21, 10:59 am, "Enigma Enlist" <dynamic-l...@hotmail.com> wrote:
> the user needs to crop the image himself a/c to the coding u suggested...if a user wants to crop half or three fourth of the original image automatically wt wud b the changing then?
-------------------------------------------------------------------
What code are you referring to?
Perhaps you should just start your own brand new thread.
Or maybe the following will answer your question.

% Read in standard MATLAB demo image.
grayImage = imread('cameraman.tif');
subplot(1,2, 1);
imshow(grayImage, []);
title('Original Grayscale Image');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

[rows columns numberOfColorBands] = size(grayImage);
height = int32(3/4 * rows);
croppedImage = imcrop(grayImage, [1 1 columns height]);
subplot(1,2, 2);
imshow(croppedImage, []);
title('Grayscale Image cropped at 3/4 the height of the original');

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
crop image wilson 28 Oct, 2009 16:14:04
rssFeed for this Thread

Contact us at files@mathworks.com