简体   繁体   English

如何计算给定颜色图像中的角点

[英]How to counts the corner points in the image of a given color

I hope you are doing well.我希望你做得很好。 I want to count the corner points in the image of a given color.我想计算给定颜色图像中的角点。 Like blue colour has two shapes how can i find and count that corner points就像蓝色有两种形状,我怎样才能找到并计算那个角点

输入图像

I have used the following code But It find out the corner of shape not color我使用了以下代码但它找出了形状的角而不是颜色

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

def cornerpoint(img):
    img = cv2.imread(img)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    dst = cv2.cornerHarris(gray,5,3,0.04)
    ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
    dst = np.uint8(dst)
    ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
    corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
    for i in range(1, len(corners)):
        print(corners[i])
    img[dst>0.1*dst.max()]=[0,0,0]
    plt.imshow(img)

cornerpoint('/content/shapes.png')

How can I proceed to count the corner points in the image of a given color?如何继续计算给定颜色图像中的角点?

In response to questions raised in the comments, here is one way to get the list of unique colors ignoring the anti-aliasing of the colors.为了回答评论中提出的问题,这里是一种获取独特颜色列表的方法,而忽略颜色的抗锯齿。

(You could also use morphology to thin your colored lines to remove the anti-aliased pixels) (您也可以使用形态学来细化彩色线条以去除抗锯齿像素)

  • Read the input读取输入
  • Reshape to 1D image of 3 channels重塑为 3 个通道的 1D 图像
  • Use np.unique to get the colors and counts使用 np.unique 获取颜色和计数
  • Zip the colors and counts压缩颜色和计数
  • Put the zip into a list将 zip 放入列表
  • Sort the zipped list on count in reverse order以相反的顺序对压缩列表进行计数
  • Print only those colors that have counts above some threshold.仅打印计数高于某个阈值的那些颜色。
  • (Note: other filters could be used to check colors against each other to be sure not too close or to remove colors near the background color. Etc) (注意:可以使用其他过滤器来检查彼此的颜色,以确保不会太接近或去除背景颜色附近的颜色。等等)

Input:输入:

在此处输入图片说明

import cv2
import numpy as np

# read image
img = cv2.imread('colored_polygons.png')

# reshape img to 1 column of 3 colors
# -1 means figure out how big it needs to be for that dimension
img2 = img.reshape(-1,3)

# get the unique colors
colors, counts = np.unique(img2, return_counts=True, axis=0)

# zip colors, counts
unique = zip(colors,counts)

# make list of color, count
cc_list = []
for color, count in unique:
    cc_list.append((color, count))
    
# function to define key as second element (count)
def takeSecond(elem):
    return elem[1]

# sort cc_list on counts
cc_list.sort(key=takeSecond, reverse=True)

# print sorted list and threshold on count
index = 0
for item in cc_list:
    color = item[0]
    count = item[1]
    if count > 5000:
        index += 1
        print("index:", index, "count:", count, "color:", color)

List of Top Unique Colors:顶级独特颜色列表:

index: 1 count: 428771 color: [255 255 255]
index: 2 count: 15735 color: [0 0 0]
index: 3 count: 9760 color: [ 14 127   0]
index: 4 count: 9160 color: [255  38   0]
index: 5 count: 8893 color: [  0   0 255]

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

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