简体   繁体   English

如何在Open CV Python中消除这些噪音?

[英]How do i eliminate these noises in Open CV Python?

hi what i want to happen is eliminate noises and only recognize the circle. 嗨,我想发生的是消除噪音,只认出圆圈。 I have these code so far: 到目前为止,我有以下代码:

import cv2
import numpy as np
import math

cap = cv2.VideoCapture(0)

while True:
    try:
        ret, frame = cap.read()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #convert from bgr to 
hsv color space

        lower = np.array([0,0,255])
        upper = np.array([255, 255, 255])

        mask = cv2.inRange(hsv, lower, upper) 

        im2, contours, hierarchy = 
cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        area = sorted(contours, key=cv2.contourArea, reverse=True)
        contour = area[0]
        (x,y),radius = cv2.minEnclosingCircle(contour)
        radius = int(radius)
        area = cv2.contourArea(contour)
        circ = 4*area/(math.pi*(radius*2)**2)
        print(circ)
    except:
        pass

    cv2.imshow('mask', mask)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

what it does is detect the brightest light and checks how circle it is. 它的作用是检测最亮的光线并检查其圆度。 What i want to happen is eliminate the noise and detect the circle only. 我想要发生的是消除噪音并仅检测到圆圈。 I hope you can help me with my code. 希望您能对我的代码有所帮助。

图片在这里

This is just an example what my program does is detects the brightest pixel. 这只是我的程序执行的一个示例,即检测最亮的像素。 Here's the original image: 这是原始图片:

原始图片

You could try to select your contour by filtering other contours that are not in your size range. 您可以尝试通过过滤尺寸范围以外的其他轮廓来选择轮廓。 U should understand that I also just started to learn python and opencv and there is probablly loads of better approaches. 您应该了解,我也刚刚开始学习python和opencv,并且可能有很多更好的方法。 Code should be somthing like this: 代码应该是这样的:

import cv2
import numpy as np
import math

cap = cv2.VideoCapture(0)

while True:
    try:
        ret, frame = cap.read()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #convert from bgr to 

        lower = np.array([0,0,255])
        upper = np.array([255, 255, 255])

        mask = cv2.inRange(hsv, lower, upper) 

        im2, contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        area = sorted(contours, key=cv2.contourArea, reverse=True)
        for number in range(0, len(area)):
            cnts = area[number]
            if 40 < len(cnts) < 80:
                contour = area[number]
                break
        (x,y),radius = cv2.minEnclosingCircle(contour)
        radius = int(radius)
        area2 = cv2.contourArea(contour)
        circ = 4*area2/(math.pi*(radius*2)**2)
        print(circ)
    except:
        pass

    cv2.imshow('mask', mask)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

U have to change the range in for loop so that it suits your purpose. U必须更改for循环的范围,使其适合您的目的。

Update: 更新:

Maybe even better...u can eliminate other contours (noises with circular criterion): 甚至更好... u可以消除其他轮廓(带有圆形准则的噪声):

import cv2
import numpy as np
import math

cap = cv2.VideoCapture(0)

while True:
    try:
        ret, frame = cap.read()
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #convert from bgr to 

        lower = np.array([0,0,255])
        upper = np.array([255, 255, 255])

        mask = cv2.inRange(hsv, lower, upper) 

        im2, contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        area = sorted(contours, key=cv2.contourArea, reverse=True)
        for number in range(0, len(area)):
            cnts = area[number]
            if 40 < len(cnts) < 80:
                contour = area[number]
                (x,y),radius = cv2.minEnclosingCircle(contour)
                radius = int(radius)
                area2 = cv2.contourArea(contour)
                circ = 4*area2/(math.pi*(radius*2)**2)
                if 0.8 < circ < 1.5:
                    rect = cv2.boundingRect(contour)
                    x,y,w,h = rect
                    cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)
                    cv2.putText(frame,'Laser point detected',(x+w+10,y+h),0,0.5,(0,255,0))
                    print(circ)
                    break       
    except:
        pass

    cv2.imshow('mask', mask)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

You can do this using Hough circle transform . 您可以使用霍夫圆变换来做到这一点。

This is the solution for this image: 这是图像的解决方案:

import cv2
import numpy as np

img = cv2.imread('stack.jpg',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
black = np.zeros_like(cimg)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

circles = np.uint16(np.around(circles))
for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(black,(i[0],i[1]),i[2],(0,255,0),1)
    # draw the center of the circle
    cv2.circle(black,(i[0],i[1]),2,(0,0,255),1)

cv2.imshow('detected circles',black)
cv2.waitKey(0)
cv2.destroyAllWindows()

output: 输出:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM