Crop an image using coordinates

I am getting a grid for an image using vertical projection.
I want to crop the image part, within the 2 of the points as one cropping image.
Can someone advice me on this?
Thanks!

4 comentarios

darova
darova el 2 de Nov. de 2019
What about find function? It can find index of point where y = 0
Anonymous26
Anonymous26 el 2 de Nov. de 2019
@darova : Can you please explain this a bit?
darova
darova el 2 de Nov. de 2019
you have data (x,y) or only image (pixels)?
Anonymous26
Anonymous26 el 2 de Nov. de 2019
I only have the image.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 2 de Nov. de 2019
I would use regionprops() to find the start and stop of each non-zero segment, then crop. Something like (untested)
props = regionprops(verticalProfile, 'PixelIdxList');
for k = 1 : length(props)
row1 = props(k).PixelIdxList(1, 1)
row2 = props(k).PixelIdxList(1, end)
croppedImage = grayImage(row1:row2, :)
imshow(croppedImage);
drawnow;
end

11 comentarios

Image Analyst
Image Analyst el 3 de Nov. de 2019
That looks like it should also work. It's just an alternative way. Is there something wrong with it?
Anonymous26
Anonymous26 el 3 de Nov. de 2019
Editada: Anonymous26 el 27 de Nov. de 2019
The image part between point 1 and 2 as one image, the image part between 2 and 3 as another image and so on.
I don't understand. It looks like the cropping is being done by this line:
subImage = i(:, startingColumns(k):endingColumns(k));
Is it not? What do you want to crop out and save as a sub-image: BW or (the badly named) i?
Anonymous26
Anonymous26 el 3 de Nov. de 2019
Editada: Anonymous26 el 27 de Nov. de 2019
I will explain further.
Cropping is done by that part. That is correct.
In the grid there are 3 points. I want the cropping to be happend as;
Point 1 - 2 = A
Point 2 - 3 = B
Image Analyst
Image Analyst el 3 de Nov. de 2019
Post the image with the paper clip icon.
Image Analyst
Image Analyst el 3 de Nov. de 2019
But you are further saving out each blob in a vertical band, like, because you labeled it, you are getting two blobs in the rightmost band. Do you want two there or one? If one, get rid of the inner loop.
Anonymous26
Anonymous26 el 3 de Nov. de 2019
I want only one there.
Hopefully you tried what I told you about removing the inner loop. Did you? If not, why not?
Here is what you should have gotten:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
% cd('F:\Matlab Research\Images\Codes');
% myFolder = 'F:\Matlab Research\Images\Codes';
myFolder = pwd;
pathname = pwd;
% [filename, pathname] = uigetfile('*.png','Select image to be read');
filename = 'image.png';
grayImage= imread(fullfile(pathname,filename));
if ndims(grayImage) > 1
% Take blue channel
grayImage = grayImage(:, :, 3);
end
grayImage=padarray(grayImage,[0 1]);
letterLocations = any(grayImage > 128, 1);
subplot(2, 2, 1);
imshow(grayImage);
subplot(2,2, 3:4);
plot(letterLocations, 'b-', 'LineWidth', 2);
title('Projection down vertically');
grid on;
% Find Rising and falling edges
d = diff(letterLocations);
startingColumns = find(d>0)
% disp (startingColumns)
endingColumns = find(d<0)
% disp (endingColumns)
% Extract each letter.
numLetters = length(startingColumns)
plotRows = ceil(sqrt(numLetters + 3))
subplot(plotRows, plotRows, 1);
imshow(grayImage);
impixelinfo;
title('Original Image', 'FontSize', fontSize);
subplot(plotRows, plotRows, 2:3);
plot(letterLocations, 'b-');
title('Projection down vertically');
grid on;
% Extract each letter.
for k = 1 : numLetters
% Get sub image of just one character...
subImage = grayImage(:, startingColumns(k):endingColumns(k));
[L, numberOfBlobsInThisBand] = bwlabel(subImage);
baseFileName = sprintf('c%d.png', k);
% Prepend the folder to make the full file name.
fullFileName = fullfile(myFolder, baseFileName);
% Do the write to disk.
% imwrite(bw, fullFileName);
subplot(plotRows, plotRows, k + 3);
imshow(subImage);
caption = sprintf('Letter %d of %d', k, numLetters);
title(caption);
end
Anonymous26
Anonymous26 el 4 de Nov. de 2019
Thank you for your response.
When i am trying the code, i am getting the following error sir.
Index in position 3 exceeds array bounds (must not exceed 1).
Error in Untitled114 (line 17)
grayImage = grayImage(:, :, 3);
It should only get to that line if it's an RGB image. What does this say?
ndims(grayImage)
whos grayImage
[rows, columns, numberOfColorChannels] = size(grayImage)
Don't use semicolons and look to see what it reports to the command window.
Anonymous26
Anonymous26 el 8 de Nov. de 2019
Thank you so much for your support. I made some modifications in the code and made it work, according to my need.
Thanks a lot again!

Iniciar sesión para comentar.

Más respuestas (1)

darova
darova el 2 de Nov. de 2019
Here is an attempt
clc,clear
I = imread('Capture.jpeg');
I1 = im2bw(I); % convert to binary
I2 = bwareaopen(~I1,100); % remove numbers (small areas)
[row,col] = find(I2);
I3 = I2*0;
h = 1; % crop thickness
ii = row(1)+h:row(end)-h;
jj = col(1)+h:col(end)-h;
I3(ii,jj) = I2(ii,jj); % crop
I4 = bwareaopen(I3,200); % remove small areas
[L,n] = bwlabel(I4); % label image
for i = 1:4
subplot(2,2,i)
I5 = L == i; % find part of a curve
I6 = imdilate(I5,ones(5)); % highlight the part
II = cat(3,~I6,I1,I1)*255;
imshow(II) % show original image and part of a curve
end
im assuming the original image has no those circles
1Capture.jpg

2 comentarios

Anonymous26
Anonymous26 el 3 de Nov. de 2019
My image is not the attached one. That is the gride i got for the original image.
After getting that gride only i am trying to crop the image using the y = 0 points.
darova
darova el 3 de Nov. de 2019
Do you like my idea?

Iniciar sesión para comentar.

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Nov. de 2019

Editada:

el 27 de Nov. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by