how crop an image automatically without any coordinate?

Hello everyone. My original image width 841pixels x height 482 pixels. So, the problem here's, I want to crop the image automatically with 512x512 pixels in the center of the lesion like the picture above. but my original image does not enough pixels to crop. Should I enlarge the original image first? What I meant is that, the pixels from the original image is enlarge from the original one. So, my objective have 2, first : if the orignal image does not enough pixels crop, I want Matlab to enlarge the pixels of the original image at first, then crop automatically into 512x512 pixels. Here, I attacth the image that I want to crop using square box. As you can see below picture, the square box detects the lesion ground truth below and crop it automatically. and also the original image I attached below.
Hope that you can help me out. Thank you.

 Accepted Answer

You can use padarray to add enough rows so that your box can be 512x512. Or you can crop what you have within the existing image and then use imresize to force the cropped image to be 512x512.

18 Comments

hi sir, could you show the sample? i got so many images to crop
Hello, I have tried this code. So far, its ok worked. But I want it now for top and bottom side. Could you please help me?
rgbImage = imread('1_245.jpg');
[heigth,width,dim] = size(rgbImage);
if heigth>width
cal=(heigth-width)/2;
calculatedWidth=double(fix(cal));
paddedImage = padarray(rgbImage,[calculatedHeigth 0],255); %swapped the elements of padsize
else
cal=(width-heigth)/2;
calculatedHeigth=double(fix(cal));
paddedImage = padarray(rgbImage,[calculatedHeigth 0],255); %swapped the elements of padsize
end
(Note: normally the word is spelled height not heigth )
Note that you do the same thing in both branches, except that in one branch you have (heigth-width) and in the other branch you have (width-heigth) .
That suggests that you could combine the branches using abs():
calculatedHeigth = floor(abs((heigth-width)/2));
paddedImage = padarray(rgbImage,[calculatedHeigth 0],255);
hello sir, thank you for your replied. All worked. but I want to ask, what if I want enlarge the tissue inner the image? What I meant is that, the inside of the image is not use 0 1 value but the tissue its in the image. Is it possible to make it? Below I show you the image that I got before.
I'm not sure what you're asking. Do you want to crop out the green outlined region? Do you have the coordinates of the green outline? I think you must since you plotted the green outline. So just use min and max to find the starting and ending columns of the green and then use indexing to get the cropped image
minRow = min(yGreen);
minRow = max(yGreen);
minCol = min(xGreen);
maxCol = max(xGreen);
croppedImage = rgbImage(minRow:maxRow, minCol:maxCol, :);
yes, exactly! i want to crop the green outline region but without any coordinate (i want it to crop automatically). i want the image to be cropped by 512x512 pixels. can you show me how?
is the codes above you gave for finding the coordinate for green outline region, right Sir? What if cropping automatically Sir without any coordinate? could matlab can do that task?
hello sir. could you please give sample to crop the green outline region? how to crop it into 512x512 pixels? with square bounding box
hello sir. could you please give sample to crop the green outline region? how to crop it into 512x512 pixels? with square bounding box
hello sir. I have already tested on your codes. its worked, but not that cropped as 512x512px that I want. as you can see below, I give example to crop the region like this I want.
how to crop the exact image like this Sir?
Why does it need to be 512x512? Are you putting it into a deep learning network and that is the size that the network requires?
In your duplicate question I showed you how to find dark regions but I don't know what distinguishes the one dark region you indicated from all the other dark regions. Can you explain why that particular region is different?
Once you have extracted the one single region you want, then you can crop it by getting the bounding box from regionprops and calling imcrop.
What do you want to do if that region is not 512x512, which it most likely won't be. Do you want to stretch/shrink the region to fit that size even if it changes the relative x and y scale? Or do you want to pad the region with zeros?
Finally do you want to crop the original image, the binary mask image, or both? And again, WHY do you think you need to do that?
Hi Sir, I will answer you one by one.
1) Yes Sir, before feed as input into deep learning network, I need to do some image processing at first that is I need to feed the images with the sizes of 512x512 pixels, because all my network used 512x512 pixels image as input.
2) Of course Sir, the others dark regions got difference with each other. The one dark region showed the suspicious area so that I need to crop that region. (the suspicious area got irregular in shape, which is the cancer targeted) and the other dark regions that does not look like suspicious area will be ignored.
3) I don't think that pad the region with zeros is proper way to fit that sizes I want. So for now, I would like to try shrink the original image first.
4) I got 2 different type images that I would to crop to. first, original image (with/out the green outline region). second, with the green outline region (called it ground truth image by radiologist). the binary mask image I will this step after the green outline region cropped. The reason WHY I need to crop this way is because my research all about segmentating the images. So, I need to fit the sizes of the image first before feed into deep learning network.
1) Image with green outline region
2) Image without green outline region
as you can see this two image, image 1) showed the binary correctly but then image 2) doest not showed incorrect one. I have tried also to find the centroid of the image with object, but seems the Matlab cannot find the centroid automatically (if use original image), the reason I use centroid is to crop easily into 256x256pixels (this time I changed the sizes of the image)
Lets say I want to resize with unchanged aspect ratio but using padding. This is my code:-
image=imread('1_245_original.jpg');
size = 256;
function new_image = letterbox_image(image, size)
% resize image with unchanged aspect ratio using padding
[iw, ih] = size(image);
[w, h] = deal(size);
scale = min(w/iw, h/ih);
nw = round(iw*scale);
nh = round(ih*scale);
image = imresize(image, [nw, nh], 'bicubic');
new_image = uint8(ones(h, w, 3)*256);
new_image((h-nh)//2+1:(h-nh)//2+nh, (w-nw)//2+1:(w-nw)//2+nw, :) = image;
end
but then, I got the error here:-
File: resize4.m Line: 14 Column: 22
Invalid use of operator.
Wow, so much wrong with that code! For one thing, don't use size as the name of your variable since it's the name of a very important built-in function. In fact you later try to use it as the built-in function to get the size of your image but since you overrode the function with your variable, it will use the variable "size" instead of the function size(). Not only that but if you were using the size function, you're using it incorrectly. JPG images are often RGB even if they appear gray scale. So you'd need to do
[rows, columns, numberOfColorChannels] = size(yourImage);
which brings up to yet another problem. "image" is also the name of a built in function so you should not use it for the name of your variable. Call in inputImage instead of image.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!