我有一张二进制图片,我想得到这张照片左下角的点数:
(红色箭头指向的那个)。特别是,我想得到那一点的坐标。
编辑: 我有这个photo,我想获得硬币的x,y坐标,并获得相对于左下角的位置。这是我的代码以及我到目前为止所做的工作。
%% loading the image
A=imread('as.jpg');
figure, imshow(A);
K=rgb2gray(A);
I=imrotate(K,90);
[H,W]=size(I);
figure, imshow(I);
%%
[visina,sirina]=size(I);
sir=sirina(1)/5;
P1=I(:,1:sir);
%% cutting the photo in segments
podelba=4;
P=cell(podelba,1);
pomN=sir;
for n=1:4
pomK=sir+pomN;
P{n}=I(:,pomN:pomK);
pomN=pomK;
end
W=imrotate(([P1 P{1} P{2} P{3} P{4} ]),0);
figure, imshow(W);
%% threshold
g1=im2bw(P1,graythresh(P1));
podelba1=4;
G=cell(podelba,1);
for i=1:4
G{i}=im2bw(P{i},graythresh(P{i}));
end
BW=~imrotate(([g1 G{1} G{2} G{3} G{4}]),0);
figure, imshow(BW);
%% noise reduction
bw=bwareaopen(BW,20);
se=strel('disk',2);
bw=imclose(bw,se);
bw=imrotate(imfill(bw,'holes'),-90);
figure, imshow(bw);
%%
[B,L] = bwboundaries(bw,'noholes');
figure, imshow(bw);
% Display the label matrix and draw each boundary
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
%%
stats = regionprops(L,'Area','Centroid');
threshold = 0.80;
% loop over the boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
% compute the roundness metric
metric = 4*pi*area/perimeter^2;
% display the results
metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle
if metric > threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
end
% text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
% 'FontSize',14,'FontWeight','bold');
end
答案 0 :(得分:1)
如果您有图像处理工具箱,则可以使用regionprops
为您提供图像中每个连接的白色区域内的像素和像素列表。
%// Load image and convert to binary
img = imread('http://i.stack.imgur.com/CAPB3.jpg');
img = img(:,:,1) > 50;
%// Determine centroids of each connected component
props = regionprops(img, {'Centroid', 'PixelIdxList'});
%// The lower left corner will have the centroid with the highest Y component
centroids = cat(1, props.Centroid);
[~, bottomLeftIdx] = max(centroids(:,2));
bottomLeft = props(bottomLeftIdx);
现在,您可以使用bottomLeft
来访问该块中包含的像素列表。
result = double(img);
result(bottomLeft.PixelIdxList) = 2;
imshow(result, [])