How to crop set of images?

3 次查看(过去 30 天)
Joseph Garrett
Joseph Garrett 2021-4-3
I have a set of many of these figures that I need to crop. I would like to crop all of them to only include the blue intensity plot area, with no scales or anything. Any help on ways to do this in MATLAB 2018b?

回答(2 个)

DGM
DGM 2021-4-3
编辑:DGM 2021-4-3
If all the images have the same geometry, you can do it directly:
inpict=imread('explot.jpg'); % i renamed the image
croppedpict=inpict(39:294,169:451,:);
You could just do it in a loop as required.
If the image geometries vary and you need to programmatically crop the desired area, the process would differ. I'm sure there are vastly better ways of doing it, but since I feel lazy, I'm going to resort to just using the tools I'm familiar with most.
inpict=imread('explot.jpg'); % i renamed the image
% creat a thresholded copy
mpict=rgb2gray(inpict)<0.3*255;
% get rid of all objects except the largest one
mpict=bwareafilt(mpict,1);
% get the location of the ROI
[~,width]=cropborder(mpict,[NaN NaN NaN NaN]);
% use that location to extract the ROI
croppedpict=cropborder(inpict,width);
imshow2('croppedpict','tools')
cropborder and imshow2 are from the MIMT on the File Exchange:
If you don't have bwareafilt() because you don't have IPT, you can use bwareafiltFB from MIMT.
EDIT: I guess I can add a non-mimt way of doing it...
% generate a simple mask to isolate the ROI
mpict=rgb2gray(inpict)<0.3*255;
mpict=bwareafilt(mpict,1);
% find the location of the object edges
rows=max(mpict,[],2);
cols=max(mpict,[],1);
y=[find(rows,1,'first') find(rows,1,'last')];
x=[find(cols,1,'first') find(cols,1,'last')];
% use that location to extract the ROI
croppedpict=inpict(y(1):y(2),x(1):x(2),:);
It works. Still uses IPT though.

Image Analyst
Image Analyst 2021-4-4
Here's a way to do it if the starting and ending rows and columns vary from image to image:
filename = '101_44-48kPWAS1.jpg'
rgbImage = imread(filename);
[r, g, b] = imsplit(rgbImage);
threshold = 128
nonWhiteMask = (r < threshold | g < threshold | b < threshold);
nonWhiteMask = imfill(nonWhiteMask, 'holes'); % Fill holes.
nonWhiteMask = bwareafilt(nonWhiteMask, 1); % Take largest blob only
% imshow(nonWhiteMask)
props = regionprops(nonWhiteMask, 'BoundingBox'); % Get bounding box coordinates.
outputImage = imcrop(rgbImage, props.BoundingBox);
imshow(outputImage); % Display final result.

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by