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

How to counts the corner points in the image of a given 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')

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

针对评论中提出的问题,这是一种忽略颜色抗锯齿的获取独特颜色列表的方法。

(您也可以使用形态学来细化彩色线条以去除抗锯齿像素)

  • 读取输入
  • 整形为 3 通道的一维图像
  • 使用np.unique获取颜色和计数
  • 压缩颜色和数量
  • 将 zip 放入列表
  • 按倒序对压缩列表进行排序
  • 只打印那些计数高于某个阈值的颜色。
  • (注意:其他滤镜可用于相互检查颜色以确保颜色不会太接近或去除接近背景颜色的颜色等)

输入:

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)

顶级独特颜色列表:

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]