Filter löschen
Filter löschen

How to crop set of images?

3 Ansichten (letzte 30 Tage)
Joseph Garrett
Joseph Garrett am 3 Apr. 2021
Beantwortet: Image Analyst am 4 Apr. 2021
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?

Antworten (2)

DGM
DGM am 3 Apr. 2021
Bearbeitet: DGM am 3 Apr. 2021
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 am 4 Apr. 2021
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.

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by