Home

gentlesnow

03 Nov 2018

【OpenCV】找圆

#!/user/bin/env python
# coding:utf-8
'''
Created on 2018-11-03
Upgrate on 2018-11-03
Anthor: Moriarty12138
Github: https://github.com/Moriarty12138/computer-vision-practice
'''
import cv2

img=cv2.imread('1.bmp',cv2.IMREAD_COLOR)
cv2.imshow("img",img)

hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow('gray',gray)

xgrad=cv2.Sobel(gray,cv2.CV_16SC1,1,0)
ygrad=cv2.Sobel(gray,cv2.CV_16SC1,0,1)

edge_output = cv2.Canny(xgrad,ygrad,50,150)
cv2.imshow("edge",edge_output)
print(edge_output)

print("---")
ret,thresh = cv2.threshold(gray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

print("tu xing ge shu %d" % (len(contours)-1))

theta = 100

for i in range(0,len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    print("di %d ge tu xing " % i)
    # cv2.rectangle(img, (x,y), (x+w,y+h), (153,153,0), 5)

    if (w-h)**2 < theta:
        print(x,y)
        newimage = img[y + 2:y + h - 2, x + 2:x + w - 2]
        cv2.imshow("new img %d" % i, newimage)

for i in range(0,len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    if (w - h) ** 2 < theta:
        for m in range(y, y + h):
            for n in range(x, x + w):
                img[m, n] = img[0, 0]
cv2.imshow("no img",img)


cv2.waitKey(0)
cv2.destroyAllWindows()
#!/user/bin/env python
# coding:utf-8
'''
Created on 2018-11-03
Upgrate on 2018-11-03
Anthor: Moriarty12138
Github: https://github.com/Moriarty12138/machine-learning-practice
'''
import cv2
import numpy as np

img=cv2.imread('11.bmp',cv2.IMREAD_COLOR)
cv2.imshow("img",img)

hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('hsv',hsv)

gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
cv2.imshow('gray',gray)

xgrad=cv2.Sobel(gray,cv2.CV_16SC1,1,0)
ygrad=cv2.Sobel(gray,cv2.CV_16SC1,0,1)

edge_output = cv2.Canny(xgrad,ygrad,50,150)
cv2.imshow("edge",edge_output)
print("edge")
print(edge_output.shape)

print("---")
ret,thresh = cv2.threshold(gray,127,255,0)
image, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

print("tu xing ge shu : %d" % (len(contours)-1))

theta = 100
RED = np.array([255,0,0])
for i in range(0,len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    print("di %d ge tu xing " % i)
    # cv2.rectangle(img, (x,y), (x+w,y+h), (153,153,0), 5)

    if (w-h)**2 < theta:
        print(x,y)
        newimage = img[y + 2:y + h - 2, x + 2:x + w - 2]
        cv2.imshow("new img %d" % i, newimage)

for i in range(0,len(contours)):
    x, y, w, h = cv2.boundingRect(contours[i])
    if (w - h) ** 2 < theta:
        cnum1 = 0
        cnum2 = 0
        cnum3 = 0
        cnum4 = 0
        cnum5 = 0

        cnum6 = 0
        cnum7 = 0
        cnum8 = 0
        cnum9 = 0
        cnum0 = 0

        for a in range(h):
            if edge_output[y+a, int(x+1/4*w)] != edge_output[y+a+1, int(x+1/4*w)]:
                cnum1 += 1
            if edge_output[y+a, int(x+3/4*w)] != edge_output[y+a+1, int(x+3/4*w)]:
                cnum2 += 1
            if edge_output[y+a, int(x+w)] != edge_output[y+a+1, int(x+w)]:
                cnum3 += 1
            if edge_output[y+a, int(x)] != edge_output[y+a+1, int(x)]:
                cnum4 += 1
            if edge_output[y+a, int(x+1/2*w)] != edge_output[y+a+1, int(x+1/2*w)]:
                cnum2 += 1

        for a in range(w):
            if edge_output[int(y+1/4*h), x+a] != edge_output[int(y+1/4*h), x+a+1]:
                cnum6 += 1
            if edge_output[int(y+3/4*h), x+a] != edge_output[int(y+3/4*h), x+a+1]:
                cnum7 += 1
            if edge_output[int(y+h), x+a] != edge_output[int(y+h), x+a+1]:
                cnum8 += 1
            if edge_output[int(y), x+a] != edge_output[int(y), x+a+1]:
                cnum9 += 1
            if edge_output[int(y+1/2*h), x+a] != edge_output[int(y+1/2*h), x+a+1]:
                cnum0 += 1
        print("-----")
        print(cnum1, cnum2, cnum3, cnum4, cnum5)
        print(cnum6, cnum7, cnum8, cnum9, cnum0)

        if cnum0 == 4:
            col = [255,0,255]
        elif cnum0 == 3 and cnum6 == 3:
            col = [0,255,255]
        else:
            col = [255,0,0]
        print("_________")
        print(img[0, 0])
        print(img[y, x])

        for m in range(y, y + h):
            for n in range(x, x + w):
                if(img[m,n] != img[0, 0]).all():
                    #print("img[m,n]")
                    #print(img[m, n])
                    img[m, n] = col

cv2.imshow("result img",img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Til next time,
gentlesnow at 17:41

scribble