Matlab 如何遍历图像上的白色像素,在每两个像素后添加节点,并使用边连接这些节点

Matlab 如何遍历图像上的白色像素,在每两个像素后添加节点,并使用边连接这些节点,matlab,image-processing,graph-theory,Matlab,Image Processing,Graph Theory,每个人我都在一个新的手写字图像图形数据库上做我的项目,我在{图形提取–关键点(节点提取)} 阿罗吉姆 Input: Skeleton image S, Distance threshold D Output: Graph g = (V, E) with nodes V and edges E 1: function Keypoint(S,D) 2: for Each connected component CC ∈ S do 3: V = V ∪ {(x, y) ∈ CC | (x, y) ar

每个人我都在一个新的手写字图像图形数据库上做我的项目,我在{图形提取–关键点(节点提取)}

阿罗吉姆

Input: Skeleton image S, Distance threshold D
Output: Graph g = (V, E) with nodes V and edges E
1: function Keypoint(S,D)
2: for Each connected component CC ∈ S do
3: V = V ∪ {(x, y) ∈ CC | (x, y) are end- or junction points}
4: Remove junction points from CC
5: for Each connected subcomponent CCsub ∈ CC do
6: V = V ∪ {(x, y) ∈ CCsub | (x, y) are points in equidistant intervals D}
7: for Each pair of nodes (u, v) ∈ V × V do
8: E = E ∪ (u, v) if the corresponding points are connected in S
9: return g 
我正在使用
bwmorph
函数查找分支点和端点以及分支点和端点之间的中点这是我的代码

clc;
clear all;
% read in a sample image -- also see letters.png, bagel.png
J=im2double(imread('i2.jpg'));

% Normalize and Binarization
b = imresize(J,[100,100]);
th = graythresh(b);
BW1 = im2bw(b, th);
figure;
imshowpair(b, BW1, 'montage');

% the standard skeletonization:
skelimg = bwmorph(~BW1,'thin',inf);

mn = bwmorph(skelimg,'branchpoints');
[row, column] = find(mn);
branchpts = [row column];
Endimg = bwmorph(skelimg,'endpoints');
[row,column] = find(Endimg);
Endpts = [row column];

n = size(Endpts,1);
Cntrpts = zeros(n,2);
for ii = 1:n
    % compute end & branch points geodesic distance transform
    dEnd = bwdistgeodesic(skelimg, Endpts(ii,2), Endpts(ii,1), 'quasi-euclidean');
    [~,closestBranchIdx] = min(dEnd(mn));   
    dStart = bwdistgeodesic(skelimg, branchpts(closestBranchIdx,2), branchpts(closestBranchIdx,1), 'quasi-euclidean');
    D = dStart + dEnd;
    D = round(D *8) / 8;
    D(isnan(D)) = inf;
    paths = imregionalmin(D);
    % compute geodesic distance on found path from end point and divide max distance by 2 for center point
    dCenter = bwdistgeodesic(paths, Endpts(ii,2), Endpts(ii,1), 'quasi-euclidean');
    dCenter(isinf(dCenter)) = nan;
    c = nanmax(dCenter(:)) / 2;
    [~,centerPointIdx] = nanmin(abs(dCenter(:) - c));
    [yc,xc] = ind2sub(size(dCenter),centerPointIdx);
    Cntrpts(ii,:) = [yc,xc];
end
n = size(branchpts,1);
Cntrpts2 = zeros(n,2);
for ii = 1:n
    % compute end & branch points geodesic distance transform
    dEnd = bwdistgeodesic(skelimg, branchpts(ii,2), branchpts(ii,1), 'quasi-euclidean');
    [~,closestBranchIdx] = min(dEnd(Endpts));   
    dStart = bwdistgeodesic(skelimg, Endpts(closestBranchIdx,2), Endpts(closestBranchIdx,1), 'quasi-euclidean');
    D = dStart + dEnd;
    D = round(D * 2) / 2;
    D(isnan(D)) = inf;
    paths = imregionalmin(D);
    % compute geodesic distance on found path from end point and divide max distance by 2 for center point
    dCenter = bwdistgeodesic(paths, branchpts(ii,2), branchpts(ii,1), 'quasi-euclidean');
    dCenter(isinf(dCenter)) = nan;
    c = nanmax(dCenter(:)) / 2;
    [~,centerPointIdx] = nanmin(abs(dCenter(:) - c));
    [yc,xc] = ind2sub(size(dCenter),centerPointIdx);
    Cntrpts2(ii,:) = [yc,xc];
end
figure;imshow(skelimg);
hold on;
plot(Cntrpts(:,2),Cntrpts(:,1),'r.')
plot(Cntrpts2(:,2),Cntrpts2(:,1),'y.')
plot(branchpts(:,2),branchpts(:,1),'g.');
plot(Endpts(:,2),Endpts(:,1),'b.');
输入图像

输出图像

期望图像


我没有在方向部分或循环中获取点,为了避免这种情况,我想在每两个像素后在图像上的白色像素上遍历,我想添加节点,我想在添加节点后将边连接到每个节点。我想在图形中获取输入图像作为我的预期图像。

以下OpenCV代码使用python编写,除了解释之外:

import cv2
filename = 'hindi.jpg'
img1 = cv2.imread(filename)  #--- Reading the image ---

img = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)    #--- Converting to grayscale ---

ret, th = cv2.threshold(img, 160, 255, 1)  #--- Binary threshold ---
cv2.imshow('th.jpg', th)

在执行膨胀时,您可以获得:

在执行膨胀时,您可以获得:


您尝试过blackhat变换吗?没有,请给我一些代码。我能够大致描述节点,但不是完美的。我还没有画出边缘之后,虽然。。。我用python在OpenCV中尝试过……我用的是matlab我不知道OpenCV和python。现在我在学习matlab并做我的项目希望它能让你开始:谢谢你的关心,但在我的算法中我想修正D值,D值,在D=2或3的意义上,我想在每插入一个节点的两个或三个像素之后遍历白色像素。在白色像素上遍历之后,我将在word图像上获得节点。在word图像上获得节点之后,我想连接相应的节点以获得更多说明。请检查我的上述算法,我想在算法的基础上实现。您可以使用
for
循环遍历,然后我想表示我的输入word图像,在我的问题中,用我期待的话来说,我仍然在挣扎
kernel = np.ones((3,3),np.uint8)
dilate = cv2.morphologyEx(th, cv2.MORPH_DILATE, kernel, 3)  #--- Morphological dilation ---
cv2.imshow('dilate.jpg', dilate)
blackhat = cv2.morphologyEx(dilate, cv2.MORPH_BLACKHAT, kernel)  #--- Performing blackhat morphology ---
cv2.imshow('blackhat.jpg', blackhat)
ret, th1 = cv2.threshold(th, 127, 255, 1)  #--- Inverting the threshold image ---
tophat = cv2.morphologyEx(th1, cv2.MORPH_TOPHAT, kernel)  #--- Performing tophat morphology ---
cv2.imshow('tophat.jpg', tophat)