'How can get the hexagons and circle in this image with opencv?
I can's find the hexagons and circle clearly in this image, in the process of finding, i have used too many normal ways but there was still have no effective result.The code which i have used as following. In the following codes, at first, through Gaussian to blurred the image, then threshold the image through the adaptive threshold method, compared with the SOBEL, SCHARR, LAPLACIAN, i found that the adaptive threshold result was best. So find the contours in the adaptive result and draw the contours in the new image. Using HoughCircles to get the circle in the image. But the hexagons was always not complete.
import cv2 as cv
import numpy as np
import argparse
import matplotlib.pyplot as plt
def get_parser():
parser=argparse.ArgumentParser(description="ALL SOLID ARGS")
parser.add_argument("--filename",default="C:/Users/JayLee/Desktop/OpenCV/contours/onsite samples/stainless steel 3.png")
parser.add_argument("--IMAGE_SIZE_K",type=float,default=0.88)
parser.add_argument("--IMAGE_K_HEIGHT_UP",type=float,default=0)
parser.add_argument("--IMAGE_K_HEIGHT_DOWN",type=float,default=1)
parser.add_argument("--IMAGE_K_WIDTH_LEFT",type=float,default=0)
parser.add_argument("--IMAGE_K_WIDTH_RIGHT",type=float,default=1)
parser.add_argument("--GAUSSIAN_SIGMA_X",type=int,default=1)
parser.add_argument("--ADAP_BLOCK_SIZE",type=int,default=3)
parser.add_argument("--ADAP_C",type=float,default=3)
parser.add_argument("--APPROX_EPSILON_K",type=float,default=0.0001)
parser.add_argument("--CRITICAL_AREA_K",type=float,default=0.01)
return parser
dilate_kernel=np.ones((3,3),np.uint8)
erode_kernel=cv.getStructuringElement(cv.MORPH_CROSS,(3,3))
parser=get_parser()
args=parser.parse_args()
src=cv.imread(args.filename)
#GET ADAPTIVE
image=cv.resize(src,None,fx=args.IMAGE_SIZE_K,fy=args.IMAGE_SIZE_K,interpolation=cv.INTER_LINEAR)
height,width=image.shape[:2]
image=image[int(height*args.IMAGE_K_HEIGHT_UP):int(height*args.IMAGE_K_HEIGHT_DOWN),
int(width*args.IMAGE_K_WIDTH_LEFT):int(width*args.IMAGE_K_WIDTH_RIGHT),:]
# cv.imshow("init_image",image)
height,width=image.shape[:2]
#GET GRAY IMAGE
gray=cv.cvtColor(image,cv.COLOR_RGB2GRAY)
g=gray/255
gamma=np.uint8((np.power(g,0.7))*255)
print("gamma=",gamma)
cv.imshow("gamma",gamma)
Imin,Imax=cv.minMaxLoc(gray)[:2]
print(Imin,Imax)
Omin,Omax=0,255
a=float((Omax-Omin)/(Imax-Imin))
b=Omin-a*Imin
gray=(a*gray+b).astype(np.uint8)
cv.imshow("gray",gray)
#GET SOBEL IMAGE
sobelx=cv.Sobel(gray,cv.CV_64F,1,0)
sobely=cv.Sobel(gray,cv.CV_64F,0,1)
sobel=cv.addWeighted(cv.convertScaleAbs(sobelx),0.5,cv.convertScaleAbs(sobely),0.5,0)
# cv.imshow("sobel",sobel)
#GET SCHARR IMAGE
scharrx=cv.convertScaleAbs(cv.Scharr(gray,cv.CV_64F,1,0))
scharry=cv.convertScaleAbs(cv.Scharr(gray,cv.CV_64F,0,1))
scharr=cv.addWeighted(scharrx,0.5,scharry,0.5,0)
# cv.imshow("scharr",scharr)
#GET LAPLACIAN IMAGE
laplacian=cv.convertScaleAbs(cv.Laplacian(gray,cv.CV_64F))
# cv.imshow("laplacian",laplacian)
# print(",,,,,",np.max(np.array(laplacian)))
#GET BLURRED IMAGE
blurred=cv.GaussianBlur(gamma,(7,7),sigmaX=args.GAUSSIAN_SIGMA_X)
#blurred=cv.blur(gray,(3,3))
# blurred=cv.boxFilter(gray,-1,(3,3),normalize=1)
# blurred=cv.medianBlur(gray,3)
# blurred=cv.bilateralFilter(gray,55,10,30)
# kernel=np.ones((3,3),np.float32)/25
# blurred=cv.filter2D(gray,-1,kernel)
cv.imshow("blurred",blurred)
#GET THRESHOLD
thresh=cv.adaptiveThreshold(blurred,255,cv.ADAPTIVE_THRESH_MEAN_C,
cv.THRESH_BINARY_INV,
blockSize=args.ADAP_BLOCK_SIZE,
C=args.ADAP_C)
# cv.imshow("thresh",thresh)
#GET IMAGE CONTOURS
cnts=cv.findContours(thresh,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)[0]
# GET APPROX CONTOURS
approx_cnts=[]
for a in cnts:
length=cv.arcLength(a,True)
approx=cv.approxPolyDP(a,args.APPROX_EPSILON_K*length,True)
approx_cnts.append(approx)
# GET CONTOURS AREA
areas=[]
areas_id=[]
for c in range(len(approx_cnts)):
area=cv.contourArea(approx_cnts[c])
areas.append(area)
areas_id.append(id)
#GET CHOICED CONTOURS
critical_area=args.CRITICAL_AREA_K*max(areas)
choiced_cnts=[]
for c in range(len(areas)):
if areas[c]>critical_area:
temp=approx_cnts[c]
choiced_cnts.append(temp)
#DRAW CHOICED CONTOURS
choiced_image=np.zeros((height,width),dtype=np.uint8)
choiced_image=cv.drawContours(choiced_image,np.array(choiced_cnts,dtype=object),-1,color=255,thickness=1)
cv.imshow("choiced iamge",choiced_image)
#CONTOURS MORPHOLOGY DEALING
temp=choiced_image.copy()
dilated=cv.dilate(temp,dilate_kernel)
# cv.imshow("dilate",dilated)
eroded=cv.erode(dilated,erode_kernel)
# cv.imshow("erode",eroded)
#GET NEW CONTOURS
new_cnts=cv.findContours(eroded,cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)[0]
new_apprxo_cnts=[]
for nc in new_cnts:
epsilon=0.01
approx=cv.approxPolyDP(nc,epsilon,True)
new_apprxo_cnts.append(approx)
temp=np.zeros((height,width),np.uint8)
for c in range(len(new_apprxo_cnts)):
if c%2==0:
print("contours {}approx points number:".format(c),len(new_apprxo_cnts[c]))
temp=cv.drawContours(temp,np.array(new_apprxo_cnts,dtype=object),c,
color=255,thickness=1)
cv.imshow("new_approx_cnts",temp)
# print(len(new_apprxo_cnts))
#CONTOURS CONVEX DETECT
#CONVEX CONTOURS DRAW
#SHOW IMAGE IN PLT
# plt.subplot(3,4,1)
# plt.imshow(cv.cvtColor(src,cv.COLOR_RGB2BGR))
# plt.title("init image")
# plt.subplot(3,4,2)
# plt.imshow(gray,cmap="gray")
# plt.title("gray image")
# plt.subplot(3,4,3)
# plt.imshow(blurred,cmap="gray")
# plt.title("blurred")
# plt.subplot(3,4,4)
# plt.imshow(thresh,cmap="gray")
# plt.title("thresh")
# plt.subplot(3,4,5)
# plt.imshow(choiced_image,cmap="gray")
# plt.title("choiced_image")
# plt.subplot(3,4,6)
# plt.imshow(dilated,cmap="gray")
# plt.title("dilated")
# plt.subplot(3,4,7)
# plt.imshow(eroded,cmap="gray")
# plt.title("eroded")
# plt.subplot(3,4,8)
# plt.imshow(temp,cmap="gray")
# plt.title("temp")
# plt.subplot(3,4,9)
# plt.imshow(sobel,cmap="gray")
# plt.title("sobel")
# plt.subplot(3,4,10)
# plt.imshow(scharr,cmap="gray")
# plt.title("scharr")
# plt.subplot(3,4,11)
# plt.imshow(laplacian,cmap="gray")
# plt.title("laplacian")
# plt.show()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|