[英]how to converting pixel coordinates x and y of an image to an image in matlab?
我有一組向量x和y,其中包含圖像的像素坐標。 而且我需要使用Matlab將這些值轉換為圖像。 如何使用這些值? 哪個功能最適合呢? 我使用的代碼是:
I = imread('D:\majorproject\image\characters\ee.jpg');
imshow(I)
BW =im2bw(I);
BW=imcomplement(BW);
imshow(BW)
dim = size(BW);
col = round(dim(2)/2)-90;
row = find(BW(:,col), 1 );
boundary = bwtraceboundary(BW,[row, col],'N');
imshow(I)
hold on;
plot(boundary(:,2),boundary(:,1),'0','LineWidth',3);
BW_filled = imfill(BW,'holes');
boundaries = bwboundaries(BW_filled);
for k=1:10
b = boundaries{k};
plot(b(:,2),b(:,1),'g','LineWidth',3);
end
從中我得到了坐標值。 謝謝。
避免使用循環,並考慮使用sub2ind
索引到您的輸出圖像中。 sub2ind
會將(x,y)
坐標轉換為線性索引,以便您可以使用單個命令對所需內容進行索引:
img = false(size(I));
img(sub2ind(size(I), y, x)) = true;
imshow(img);
假設它們從1開始,此處x
和y
表示列和行坐標。如果x
和y
是行和列坐標,只需交換輸入參數:
img = false(size(I));
img(sub2ind(size(I), x, y)) = true;
imshow(img);
另外, I
是您的圖像,已使用imread
讀取。 當您想要具有與I
相同尺寸的圖像時,我們當然可以利用這一事實來創建您的輸出圖像。
或者,您可以使用sparse
函數直接索引到矩陣並將位置設置為1,然后轉換回full
logical
矩陣:
img = sparse(y, x, true, size(I,1), size(I,2));
%img = sparse(x, y, true, size(I,1), size(I,2)); %// Use this if x is row and y is column
img = full(img);
imshow(img);
由於您尚未提供像素的強度,因此我假設您想創建一個二進制圖像,您可以輕松地執行以下操作:
img=[];
for k=1:length(x) //assuming the length of x and y are same
img(x(k),y(k))=1;
end
imshow(img);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.