"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

May 30, 2018

Day #110 - Image Processing - Line Counting from Images

Learning's from recent exposure to working on images, texture and identifying the line count in vertical and horizontal axis. OpenCV was useful to arrive at different approaches

from __future__ import division
import cv2
import numpy as np
import os
#Initalize directory to images
path_to_folder = "E:\\New folder"
#Initialize different filesets
path_to_file_approach1 = "E:\\Approach1.csv"
path_to_file_approach2 = "E:\\Approach2.csv"
path_to_file_approach3 = "E:\\Approach3.csv"
path_to_file_approach4 = "E:\\Approach4.csv"
#Initialize output file for each approach
def InitializeOutputfile(ApproachCode):
if(ApproachCode == '01'):
filehandle = open(path_to_file_approach1,'w')
return filehandle
if(ApproachCode == '02'):
filehandle = open(path_to_file_approach2,'w')
return filehandle
if(ApproachCode == '03'):
filehandle = open(path_to_file_approach3,'w')
return filehandle
if(ApproachCode == '04'):
filehandle = open(path_to_file_approach4,'w')
return filehandle
#Compute EPI DPI
def ComputeDPI(ApproachCode, filehandle):
files = []
for i in os.listdir(path_to_folder):
files.append(i)
print(i)
if(ApproachCode == '01'):
EP_DPI = Approach1(i)
print(EP_DPI)
filehandle.write(i + "," + str(EP_DPI) + "\n")
if(ApproachCode == '02'):
EP_DPI = Approach2(i)
print(EP_DPI)
filehandle.write(i + "," + str(EP_DPI) + "\n")
if(ApproachCode == '03'):
EP_DPI = Approach3(i)
print(EP_DPI)
filehandle.write(i + "," + str(EP_DPI) + "\n")
if(ApproachCode == '04'):
EP_DPI = Approach4(i)
print(EP_DPI)
filehandle.write(i + "," + str(EP_DPI) + "\n")
filehandle.close()
def Approach1(img):
c = 0.00
img = cv2.imread(path_to_folder + "\\" + img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gaussian1 = cv2.GaussianBlur(gray,(3,3),0)
img_gaussian = img_gaussian1[100:400, 100:400]
img_sobelx = cv2.Sobel(img_gaussian,cv2.CV_8U,1,0,ksize=5)
img_sobely = cv2.Sobel(img_gaussian,cv2.CV_8U,0,1,ksize=5)
#Compute lines using Hough Transformation
xlines = cv2.HoughLines(img_sobelx,1,np.pi/180,200)
ylines = cv2.HoughLines(img_sobely,1,np.pi/180,200)
if(ylines is not None):
if(xlines is not None):
print('sobel - HoughLines')
c = ylines.size/xlines.size
return c
def Approach2(img):
c = 0.00
img = cv2.imread(path_to_folder + "\\" + img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gaussian1 = cv2.GaussianBlur(gray,(3,3),0)
img_gaussian = img_gaussian1[100:400, 100:400]
img_sobelx = cv2.Sobel(img_gaussian,cv2.CV_8U,1,0,ksize=5)
img_sobely = cv2.Sobel(img_gaussian,cv2.CV_8U,0,1,ksize=5)
#Erosion Followed by Dialiton
kernel = np.ones((5,5),np.uint8)
sobelErosionDialotionx = cv2.morphologyEx(img_sobelx, cv2.MORPH_OPEN, kernel)
sobelErosionDialotiony = cv2.morphologyEx(img_sobely, cv2.MORPH_OPEN, kernel)
#Compute lines using Hough Transformation
xlines = cv2.HoughLines(sobelErosionDialotionx,1,np.pi/180,100)
ylines = cv2.HoughLines(sobelErosionDialotiony,1,np.pi/180,100)
if(ylines is not None):
if(xlines is not None):
print('sobel - Erosion Followed by Dialiton')
c = ylines.size/xlines.size
return c
def Approach3(img):
c = 0.00
img = cv2.imread(path_to_folder + "\\" + img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gaussian1 = cv2.GaussianBlur(gray,(3,3),0)
img_gaussian = img_gaussian1[100:400, 100:400]
img_sobelx = cv2.Sobel(img_gaussian,cv2.CV_8U,1,0,ksize=5)
img_sobely = cv2.Sobel(img_gaussian,cv2.CV_8U,0,1,ksize=5)
#Dilation followed by Erosion
kernel = np.ones((5,5),np.uint8)
sobelDialotionErosionx = cv2.morphologyEx(img_sobelx, cv2.MORPH_CLOSE, kernel)
sobelDialotionErosiony = cv2.morphologyEx(img_sobely, cv2.MORPH_CLOSE, kernel)
xlines = cv2.HoughLines(sobelDialotionErosionx,1,np.pi/180,100)
ylines = cv2.HoughLines(sobelDialotionErosiony,1,np.pi/180,100)
if(ylines is not None):
if(xlines is not None):
print('sobel - Dilation followed by Erosion')
c = ylines.size/xlines.size
return c
def Approach4(img):
c = 0.00
minLineLength = 200
maxLineGap = 10
img = cv2.imread(path_to_folder + "\\" + img)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img_gaussian1 = cv2.GaussianBlur(gray,(3,3),0)
img_gaussian = img_gaussian1[100:400, 100:400]
img_sobelx = cv2.Sobel(img_gaussian,cv2.CV_8U,1,0,ksize=5)
img_sobely = cv2.Sobel(img_gaussian,cv2.CV_8U,0,1,ksize=5)
#Compute lines using Hough Transformation
xlines = cv2.HoughLinesP(img_sobelx,1,np.pi/180,15,minLineLength,maxLineGap)
ylines = cv2.HoughLinesP(img_sobely,1,np.pi/180,15,minLineLength,maxLineGap)
if(ylines is not None):
if(xlines is not None):
print('sobel - HoughLinesP')
c = ylines.size/xlines.size
return c
filehandle = InitializeOutputfile("01")
ComputeDPI("01", filehandle)
filehandle = InitializeOutputfile("02")
ComputeDPI("02", filehandle)
filehandle = InitializeOutputfile("03")
ComputeDPI("03", filehandle)
filehandle = InitializeOutputfile("04")
ComputeDPI("04", filehandle)
view raw Lines_Images.py hosted with ❤ by GitHub
Happy Coding!!!

No comments: