Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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_Image Processing_Computer Vision_Convolution_Scikit Image - Fatal编程技术网

Python 图像处理-消除弧形污迹

Python 图像处理-消除弧形污迹,python,image-processing,computer-vision,convolution,scikit-image,Python,Image Processing,Computer Vision,Convolution,Scikit Image,我正在处理这种形象 鞋面是后处理的 下半身是生的 首先,我将灰度图像转换成纯黑白二值图像。我对检测白色斑点感兴趣,想去除角落里的弧形污迹。我该怎么做 一般来说,我知道我的目标形状几乎是圆形的,不是太大,但我想对一些东西进行编码,以自动消除其他一切,比如左上角和右上角的较轻圆弧 在python中如何实现这一点,理想情况下是skimage?您可以使用skimage的方法hough_circle和hough_circle_peaks检测出大小合适的圆并将其切掉 在这里,我根据你的另一个问题做了如下调

我正在处理这种形象

鞋面是后处理的

下半身是生的 首先,我将灰度图像转换成纯黑白二值图像。我对检测白色斑点感兴趣,想去除角落里的弧形污迹。我该怎么做

一般来说,我知道我的目标形状几乎是圆形的,不是太大,但我想对一些东西进行编码,以自动消除其他一切,比如左上角和右上角的较轻圆弧


在python中如何实现这一点,理想情况下是skimage?

您可以使用skimage的方法hough_circle和hough_circle_peaks检测出大小合适的圆并将其切掉

在这里,我根据你的另一个问题做了如下调整:

# skimage version 0.14.0

import math
import numpy as np
import matplotlib.pyplot as plt

from skimage import color
from skimage.io import imread
from skimage.transform import hough_circle, hough_circle_peaks
from skimage.feature import canny
from skimage.draw import circle
from skimage.util import img_as_ubyte

INPUT_IMAGE = 'dish1.png' # input image name
BEST_COUNT = 1            # how many circles to detect (one dish)
MIN_RADIUS = 100          # min radius of the Petri dish
MAX_RADIUS = 122          # max radius of the Petri dish (in pixels)
LARGER_THRESH = 1.2       # circle is considered significantly larger than another one if its radius is at least so much bigger
OVERLAP_THRESH = 0.1      # circles are considered overlapping if this part of the smaller circle is overlapping

def circle_overlap_percent(centers_distance, radius1, radius2):
    '''
    Calculating the percentage area overlap between circles
    See Gist for comments:
        https://gist.github.com/amakukha/5019bfd4694304d85c617df0ca123854
    '''
    R, r = max(radius1, radius2), min(radius1, radius2)
    if centers_distance >= R + r:
        return 0.0
    elif R >= centers_distance + r:
        return 1.0
    R2, r2 = R**2, r**2
    x1 = (centers_distance**2 - R2 + r2 )/(2*centers_distance)
    x2 = abs(centers_distance - x1)
    y = math.sqrt(R2 - x1**2)
    a1 = R2 * math.atan2(y, x1) - x1*y
    if x1 <= centers_distance:
        a2 = r2 * math.atan2(y, x2) - x2*y
    else:
        a2 = math.pi * r2 - a2
    overlap_area = a1 + a2
    return overlap_area / (math.pi * r2)

def circle_overlap(c1, c2):
    d = math.sqrt((c1[0]-c2[0])**2 + (c1[1]-c2[1])**2)
    return circle_overlap_percent(d, c1[2], c2[2])

def inner_circle(cs, c, thresh):
    '''Is circle `c` is "inside" one of the `cs` circles?'''
    for dc in cs:
        # if new circle is larger than existing -> it's not inside
        if c[2] > dc[2]*LARGER_THRESH: continue
        # if new circle is smaller than existing one...
        if circle_overlap(dc, c)>thresh:
            # ...and there is a significant overlap -> it's inner circle
            return True
    return False

# Load picture and detect edges
image = imread(INPUT_IMAGE, 1)
image = img_as_ubyte(image)
edges = canny(image, sigma=3, low_threshold=10, high_threshold=50)

# Detect circles of specific radii
hough_radii = np.arange(MIN_RADIUS, MAX_RADIUS, 2)
hough_res = hough_circle(edges, hough_radii)

# Select the most prominent circles (in order from best to worst)
accums, cx, cy, radii = hough_circle_peaks(hough_res, hough_radii)

# Determine BEST_COUNT circles to be drawn
drawn_circles = []
for crcl in zip(cy, cx, radii):
    # Do not draw circles if they are mostly inside better fitting ones
    if not inner_circle(drawn_circles, crcl, OVERLAP_THRESH):
        # A good circle found: exclude smaller circles it covers
        i = 0
        while i<len(drawn_circles):
            if circle_overlap(crcl, drawn_circles[i]) > OVERLAP_THRESH:
                t = drawn_circles.pop(i)
            else:
                i += 1
        # Remember the new circle
        drawn_circles.append(crcl)
    # Stop after have found more circles than needed
    if len(drawn_circles)>BEST_COUNT:
        break

drawn_circles = drawn_circles[:BEST_COUNT]

# Draw circle and cut it out
colors  = [(250, 0, 0), (0, 250, 0), (0, 0, 250)]
fig, ax = plt.subplots(ncols=1, nrows=3, figsize=(10, 4))
color_image = color.gray2rgb(image)
black_image = np.zeros_like(image)
for center_y, center_x, radius in drawn_circles[:1]:
    circy, circx = circle(center_y, center_x, radius, image.shape)
    color = colors.pop(0)
    color_image[circy, circx] = color
    black_image[circy, circx] = image[circy, circx]
    colors.append(color)

# Output
ax[0].imshow(image, cmap=plt.cm.gray)        # original image
ax[1].imshow(color_image)                    # detected circle
ax[2].imshow(black_image, cmap=plt.cm.gray)  # cutout
plt.show()
输出:


同样,正如我前面的回答一样,这里的大多数代码都在进行层次计算,以找到最大的最佳拟合圆。

您是否可以控制图像的拍摄?如果是这样的话,你也许可以把盘子放在一个背景上,这个背景与盘子的对比度更高,光线更均匀,没有阴影?我个人没有控制权,但我会尽量把这个反馈给其他人。谢谢