Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何屏蔽图像中的灰色噪声?_Python_Opencv_Image Processing_Computer Vision - Fatal编程技术网

Python 如何屏蔽图像中的灰色噪声?

Python 如何屏蔽图像中的灰色噪声?,python,opencv,image-processing,computer-vision,Python,Opencv,Image Processing,Computer Vision,我有下面的原始图像,我想遮罩。我只希望圆形(几乎)橙色/棕色的结构是白色的。我该怎么做呢 我已经尝试过阈值化,但我不希望较低的阈值是一个变量。您可以尝试转换为HSV颜色空间和颜色阈值。但是,您可能无法将阈值作为变量删除,因为每个图像的照明都有细微的变化。根据经验,我可以告诉你,有时候你可以慷慨地扩大门槛,以满足你想要的大部分东西。但更普遍的解决方案需要更复杂的算法 从opencv文档: 11 # Convert BGR to HSV 12 hsv = cv2.cvtColor(

我有下面的原始图像,我想遮罩。我只希望圆形(几乎)橙色/棕色的结构是白色的。我该怎么做呢


我已经尝试过阈值化,但我不希望较低的阈值是一个变量。

您可以尝试转换为HSV颜色空间和颜色阈值。但是,您可能无法将阈值作为变量删除,因为每个图像的照明都有细微的变化。根据经验,我可以告诉你,有时候你可以慷慨地扩大门槛,以满足你想要的大部分东西。但更普遍的解决方案需要更复杂的算法

从opencv文档:

11     # Convert BGR to HSV
12     hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
13 
14     # define range of blue color in HSV
15     lower_blue = np.array([110,50,50])
16     upper_blue = np.array([130,255,255])
17 
18     # Threshold the HSV image to get only blue colors
19     mask = cv2.inRange(hsv, lower_blue, upper_blue) 

对于黄色色调,您当然需要调整参数。

您可以尝试转换为HSV颜色空间和颜色阈值。但是,您可能无法将阈值作为变量删除,因为每个图像的照明都有细微的变化。根据经验,我可以告诉你,有时候你可以慷慨地扩大门槛,以满足你想要的大部分东西。但更普遍的解决方案需要更复杂的算法

从opencv文档:

11     # Convert BGR to HSV
12     hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
13 
14     # define range of blue color in HSV
15     lower_blue = np.array([110,50,50])
16     upper_blue = np.array([130,255,255])
17 
18     # Threshold the HSV image to get only blue colors
19     mask = cv2.inRange(hsv, lower_blue, upper_blue) 

当然,对于黄色色调,您必须调整参数。

使用Hough circle transform找到分隔眼睛和灰色区域的圆

其基本思想是运行Hough circle transfor,然后找到圆内外值差异最大的圆

结果是:

守则:

import cv2
import numpy as np


# Read image
Irgb = cv2.imread('eye.jpg')

# Take the first channel ( No specifc reason just good contrast between inside the eye and outside)
Igray = Irgb[:,:,0]

# Run median filter to reduce noise
IgrayFilter = cv2.medianBlur(Igray,101)

# Find circles using hough circles
minRadius = np.floor(np.min(Igray.shape)/2)
circles = cv2.HoughCircles(IgrayFilter, cv2.HOUGH_GRADIENT, dp=0.5,param1=100,param2=50,minRadius=int(minRadius),minDist=100)
circles = np.uint16(np.around(circles))
cimg = Irgb

# For each circle that we found find the intinestiy values inside the circle and outside.
# We eould take the circle that as the biggest difference between inside and outside
diff = []
for i in circles[0, :]:

    # Create mask from circel identity
    mask = np.zeros_like(Igray)
    maskInverse = np.ones_like(Igray)

    cv2.circle(mask, (i[0], i[1]), i[2], 1, cv2.FILLED)
    cv2.circle(maskInverse, (i[0], i[1]), i[2], 0, cv2.FILLED)

    # Find values inside mask and outside
    insideMeanValues = np.mean(np.multiply(mask,Igray))
    outsideMeanValues = np.mean(np.multiply(maskInverse, Igray))

    # Save differnses
    diff.append(abs(insideMeanValues-outsideMeanValues))


# Take the circle with the biggest difference in color as the border circle
circleID = np.argmax(diff)
circleInfo = circles[0, circleID]

# Create mask from final image
mask = np.zeros_like(Igray)
cv2.circle(mask, (i[0], i[1]), i[2], 1, cv2.FILLED)

# Show final image only in the mask
finalImage = Irgb
finalImage[:,:,0] = np.multiply(finalImage[:,:,0],mask)
finalImage[:,:,1] = np.multiply(finalImage[:,:,1],mask)
finalImage[:,:,2] = np.multiply(finalImage[:,:,2],mask)

cv2.imwrite('circle.jpg',finalImage)

使用Hough圆变换找到将眼睛和灰色区域分开的圆

其基本思想是运行Hough circle transfor,然后找到圆内外值差异最大的圆

结果是:

守则:

import cv2
import numpy as np


# Read image
Irgb = cv2.imread('eye.jpg')

# Take the first channel ( No specifc reason just good contrast between inside the eye and outside)
Igray = Irgb[:,:,0]

# Run median filter to reduce noise
IgrayFilter = cv2.medianBlur(Igray,101)

# Find circles using hough circles
minRadius = np.floor(np.min(Igray.shape)/2)
circles = cv2.HoughCircles(IgrayFilter, cv2.HOUGH_GRADIENT, dp=0.5,param1=100,param2=50,minRadius=int(minRadius),minDist=100)
circles = np.uint16(np.around(circles))
cimg = Irgb

# For each circle that we found find the intinestiy values inside the circle and outside.
# We eould take the circle that as the biggest difference between inside and outside
diff = []
for i in circles[0, :]:

    # Create mask from circel identity
    mask = np.zeros_like(Igray)
    maskInverse = np.ones_like(Igray)

    cv2.circle(mask, (i[0], i[1]), i[2], 1, cv2.FILLED)
    cv2.circle(maskInverse, (i[0], i[1]), i[2], 0, cv2.FILLED)

    # Find values inside mask and outside
    insideMeanValues = np.mean(np.multiply(mask,Igray))
    outsideMeanValues = np.mean(np.multiply(maskInverse, Igray))

    # Save differnses
    diff.append(abs(insideMeanValues-outsideMeanValues))


# Take the circle with the biggest difference in color as the border circle
circleID = np.argmax(diff)
circleInfo = circles[0, circleID]

# Create mask from final image
mask = np.zeros_like(Igray)
cv2.circle(mask, (i[0], i[1]), i[2], 1, cv2.FILLED)

# Show final image only in the mask
finalImage = Irgb
finalImage[:,:,0] = np.multiply(finalImage[:,:,0],mask)
finalImage[:,:,1] = np.multiply(finalImage[:,:,1],mask)
finalImage[:,:,2] = np.multiply(finalImage[:,:,2],mask)

cv2.imwrite('circle.jpg',finalImage)

这是针对一般用例的吗?这是针对一般用例的吗?