フィルターのクリア

How can I centre points of interest in an image before it's cropped and saved?

2 ビュー (過去 30 日間)
Teshan Rezel
Teshan Rezel 2020 年 2 月 19 日
コメント済み: darova 2020 年 2 月 19 日
Hi folks,
I've successfully managed to crop segmented elements from an image into several images, but I'm now trying to reduce as much of the black background as possible. Below are my code, the base image with the segmented mesh and an example of a cropped section. Any help would be appreciated!
clc; close all;
img = imread('29_11_2019_164105.jpg');
pathName = "C:\Users\ezxtg4\Downloads\JPEG pics\crops\";
[counts, ~] = imhist(rgb2gray(img), 255);
T = otsuthresh(counts);
BW = imbinarize(rgb2gray(img), T);
BW2 = bwareaopen(BW, 3000);
BW2 = imfill(BW2, 'holes');
BW2 = bwperim(BW2);
BW2 = imdilate(BW2, ones(5));
BW2 = imerode(BW2, ones(3));
[L, n] = bwlabel(BW2);
for i = 1:n
I4 = L==i;
I5 = imfill(I4,'holes');
I6 = img.*repmat(uint8(I5),[1 1 3]);
I6 = imclearborder(~I6);
imwrite(I6, fullfile(pathName, sprintf('crop %d.jpeg', i)));
end
  4 件のコメント
darova
darova 2020 年 2 月 19 日
You want to crop the image or to center?
Teshan Rezel
Teshan Rezel 2020 年 2 月 19 日
sorry I wasn't clear about that! I'd like to perform the former operation, so cropping please

サインインしてコメントする。

採用された回答

darova
darova 2020 年 2 月 19 日
編集済み: darova 2020 年 2 月 19 日
You previous code was ok i think. Just made some changes
[ii,jj] = find(I5);
% calculate boundaries
i1 = min(ii); % min row
i2 = max(ii); % max row
j1 = min(jj); % min column
j2 = max(jj); % max column
% ...
I7 = imcrop(I6,[j1 i1 j2-j1 i2-i1]); % cropped image
EDITED: made a mistake in imcrop(I,[xmin ymin width height])
  2 件のコメント
Teshan Rezel
Teshan Rezel 2020 年 2 月 19 日
thank you!
darova
darova 2020 年 2 月 19 日
Made a mistake. Look at the script

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by