matlab - how to traverse on white pixels on image and add nodes after every two pixel and connect that nodes using edges -
every 1 doing project on novel graph database handwritten word images in doing on {graph extraction – keypoint (node extraction)}
alogithm
input: skeleton image s, distance threshold d output: graph g = (v, e) nodes v , edges e 1: function keypoint(s,d) 2: each connected component cc ∈ s 3: v = v ∪ {(x, y) ∈ cc | (x, y) end- or junction points} 4: remove junction points cc 5: each connected subcomponent ccsub ∈ cc 6: v = v ∪ {(x, y) ∈ ccsub | (x, y) points in equidistant intervals d} 7: each pair of nodes (u, v) ∈ v × v 8: e = e ∪ (u, v) if corresponding points connected in s 9: return g
i finding branch points , end points , mid points in between branch points , end points using bwmorph
function code.
clc; clear all; % read in sample image -- see letters.png, bagel.png j=im2double(imread('i2.jpg')); % normalize , binarization b = imresize(j,[100,100]); th = graythresh(b); bw1 = im2bw(b, th); figure; imshowpair(b, bw1, 'montage'); % 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); 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 end point , divide max distance 2 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); 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 end point , divide max distance 2 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.');
output image
am not getting points in orientation parts or in loop avoid want traverse on white pixel on image after every 2 pixel want add nodes , want connect edges every nodes after adding nodes want input image in graph expected image 3.
the following opencv code in python, alongside explanation:
import cv2 filename = 'hindi.jpg' img1 = cv2.imread(filename) #--- reading image --- img = cv2.cvtcolor(img1,cv2.color_bgr2gray) #--- converting 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)
on performing dilation can get:
ret, th1 = cv2.threshold(th, 127, 255, 1) #--- inverting threshold image --- tophat = cv2.morphologyex(th1, cv2.morph_tophat, kernel) #--- performing tophat morphology --- cv2.imshow('tophat.jpg', tophat)
on performing dilation can get:
Comments
Post a Comment