Как обрезать ч / б эти пять точек в Matlab? - PullRequest
0 голосов
/ 09 апреля 2019

Я хочу обрезать изображение внутри этих пяти пунктов Я сделал некоторый код, но это не сработало.

Я хочу обрезать изображение внутри этих пяти точек:

binary image with 5 points

bw=baseimage;
cc=regionprops(bw,'Centroid');
fixedPoints=cat(1,cc.Centroid);

bx1=floor(fixedPoints(1));
bx5=floor(fixedPoints(5));
by1=floor(fixedPoints(6));
by5=floor(fixedPoints(10));
base_crop=imcrop(n_im_base,[bx1 by1 bx5 by5]);
figure,imshow(base_crop);

Ответы [ 2 ]

1 голос
/ 09 апреля 2019

Имкроп принимает следующий аргумент imcrop(bw,[xmin ymin width height]).

Итак, вам нужно определить xmin, ymin, width и height

%We load the bw picture
bw = im2bw(imread('im.jpg'));
%Determine the centroid
s = regionprops(bw,'centroid');
%struct to matrice
c = reshape([s.Centroid],2,length(s)).'
%crop the image: min(c) will determine xmin and ymin, and max(c)-min(c) will determine the width and height
I = imcrop(bw,[min(c) max(c)-min(c)]);
imshow(I);

enter image description here

Если ваши 5 точек не образуют прямоугольник, вы также можете создать маску, чтобы показать только интересующую область:

%returns the 2-D convex hull of the points (X,Y)
k = convhull(c(:,1),c(:,2));
%create the mask
mask = poly2mask(c(k,1),c(k,2),size(bw,1),size(bw,2));
0 голосов
/ 10 апреля 2019

Вот решение, которое не требует Инструментов обработки изображений и может быть более простым.

% Read input image
img = imread('swq1I.jpg');

% Get rid of JPG artifacts ("im2bw", "imbinarize")
img(img < 100) = 0;
img(img > 100) = 255;

figure(1);
imshow(img);

% Row and column wise summation for detection of xmin, etc. 
x = sum(img, 1);
y = sum(img, 2);

% First and last non-zero elements per row and column describe the bounding rect
xmin = find(x, 1, 'first');
xmax = find(x, 1, 'last');
ymin = find(y, 1, 'first');
ymax = find(y, 1, 'last');

% Crop by simple array indexing
crop = img(ymin:ymax, xmin:xmax);

figure(2);
imshow(crop);

Результат выглядит следующим образом:

Output

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...