本文介绍了计算机视觉:Opencv计算大圆圈内的小圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我一直在处理
的图像

Here is the image on which i have been working on

目标是检测内部的小圆圈

The goal is to detect small circles inside the big one.

当前我所做的是将图像转换为灰度并应用了阈值(cv2.THRESH_OTSU),从而导致该图像

currently what i have done is converted the image to gray scale and applied threshold (cv2.THRESH_OTSU) to which resulted in this image

在此之后,我使用findcontours滤除了大对象,使用了我在stackoverflow上找到的椭圆形内核打开了Morph打开

After this i have filtered out large objects using findcontours applied Morph open using elliptical shaped kernel which i found on stackoverflow

结果图像是这样的

The result image is like this

有人可以指导我吗?通过正确的路径进行操作以及哪里出错了。

Can someone guide me through the correct path on what to do and where i'm getting wrong.

下面是我一直在处理的代码

Below is attached code on which i have been working on

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('01.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#cv2.imwrite('thresh.jpg', thresh)

# Filter out large non-connecting objects
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    #print(area)
    if area < 200 and area > 0:
        cv2.drawContours(thresh,[c],0,0,-1)

# Morph open using elliptical shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

# Find circles
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    area = cv2.contourArea(c)
    if area > 20 and area < 50:
        ((x, y), r) = cv2.minEnclosingCircle(c)
        cv2.circle(image, (int(x), int(y)), int(r), (36, 255, 12), 2)

cv2.namedWindow('orig', cv2.WINDOW_NORMAL)
cv2.imshow('orig', thresh)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.waitKey()

谢谢!

推荐答案

通过将图像转换为灰度,您浪费了很多有用的信息。

You throw away a lot of useful information by converting your image to grayscale.

为什么不使用您所寻找的斑点是唯一的红色/橙色这一事实呢?

Why not use the fact that the spots you are looking for are the only thing that is red/orange?

我将saturaton通道乘以给我这张图片的红色通道:

I multiplied the saturaton channel with the red channel which gave me this image:

现在发现白色斑点变得微不足道了

Now finding the white blobs becomes trivial.

这些渠道的权重不同的实验,或先应用阈值。有很多方法。在不同的光照,不同的背景下进行实验,直到获得用于图像处理的理想输入为止。

Experiment with different weights for those channels, or apply thresholds first. There are many ways. Experiment with different illumination, different backgrounds until you get the ideal input for your image processing.

这篇关于计算机视觉:Opencv计算大圆圈内的小圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 20:52