I have applied watershed algorithm to segment characters of an ancient writing. Now How to get the segmented characters as output?
I am working on segmenting characters from ancient inscriptions to build an OCR. I have tried MATLAB before but I am not getting sufficient results. Then I learnt about watershed algorithm, got this code from their documentation. I applied the code, It runs but doesn't output anything.
import cv2 import numpy as np from matplotlib import pyplot as plt #Load image in grayscale img = cv2.imread('templegs.png',0) kernel = np.ones((1,1),np.uint8) opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) blur = cv2.GaussianBlur(opening,(1,1),0) ret3,th4 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) # sure background area sure_bg = cv2.dilate(opening,kernel,iterations=1) # Finding sure foreground area dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,3) dist_transform = np.uint8(dist_transform) ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0) sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg,sure_fg) ret, markers = cv2.connectedComponents(sure_fg) markers = markers+1 markers[unknown==255] = 0 markers = markers.astype('int32') #now load same image as color image img = cv2.imread('temple.jpg',1) markers = cv2.watershed(img,markers) img[markers == -1] = [255,0,0]
I expect an image output.