简体   繁体   English

如何掩盖图像以消除灰色噪声?

[英]How to mask an image out of gray noise?

I have the following raw image that I want to mask. 我有以下要屏蔽的原始图像。 I want just the circular shaped (almost) orange/brown structure to be masked white. 我只希望将圆形(几乎)橙色/棕色结构蒙版为白色。 How do I go about doing it? 我该怎么做呢?

http://imgur.com/a/HNmRn http://imgur.com/a/HNmRn

I've tried thresholding, but I don't want the lower threshold value to be a variable. 我已经尝试过阈值化,但是我不希望下阈值成为变量。

You could try converting into HSV colorspace and threshold for color. 您可以尝试转换为HSV颜色空间和颜色阈值。 But you might not be able to remove the threshold as a variable, as every image has slight variations in the lighting. 但是您可能无法将阈值作为变量删除,因为每张图像的光照都会略有变化。 From experience I can tell you that sometimes you can generously extend the threshold to fit most of the stuff you want. 根据经验,我可以告诉您,有时您可以慷慨地扩大门槛,以适应您想要的大多数东西。 But a more general solution will take more sophisticated algorithms. 但是,更通用的解决方案将采用更复杂的算法。

from opencv documentation: 来自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) 

For the yellowish tone you have there you will have to adjust the parameters of course. 对于那里的淡黄色调,您当然必须调整参数。

Use Hough circle transform to find the the circle that separate the eye and the gray area. 使用霍夫圆变换找到将眼睛和灰色区域分开的圆。

The basic idea is to run Hough circle transfor and then finding the circle that has the biggest difference in values between the inside of the circles and outside. 基本思想是运行霍夫圆变换,然后找到在圆的内部与外部之间的值差异最大的圆。

The result: 结果: 在此处输入图片说明

The code: 编码:

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)

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

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