如何遍历图像上的白色像素并在每两个像素后添加节点并使用边连接这些节点
how to traverse on white pixels on image and add nodes after every two pixel and connect that nodes using edges
每个人我都在做关于手写文字图像的新型图形数据库的项目,因为我正在做{图形提取 – 关键点(节点提取)}
算法
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.');
输入图片
输出图像
预期图像
我没有在方向部分或循环中获取点以避免这种情况我想在每两个像素之后遍历图像上的白色像素我想添加节点并且我想在添加我想要的节点后将边缘连接到每个节点将图形中的输入图像作为我的预期图像 3.
以下 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)
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)
在执行扩张时你可以得到:
每个人我都在做关于手写文字图像的新型图形数据库的项目,因为我正在做{图形提取 – 关键点(节点提取)}
算法
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.');
输入图片
输出图像
预期图像
我没有在方向部分或循环中获取点以避免这种情况我想在每两个像素之后遍历图像上的白色像素我想添加节点并且我想在添加我想要的节点后将边缘连接到每个节点将图形中的输入图像作为我的预期图像 3.
以下 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)
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)
在执行扩张时你可以得到: