【问题标题】:How to counts the corner points in the image of a given color如何计算给定颜色图像中的角点
【发布时间】:2021-12-27 17:14:00
【问题描述】:

我希望你做得很好。 我想计算给定颜色图像中的角点。就像蓝色有两种形状我怎么能找到和计算角点

我使用了以下代码,但它发现形状的角不是颜色

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')

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

【问题讨论】:

  • 这是实物图还是说明图?
  • @YvesDaoust 这是真实的图像我要处理这个图像
  • @Piglet 但这就像我不想使用它的跟踪栏
  • 对颜色的过滤并不太难(擦除所有不是很接近纯色的像素)。

标签: python opencv image-processing opencv-contour


【解决方案1】:

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

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

  • 读取输入
  • 重塑为 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]

【讨论】:

  • 感谢您的回答
猜你喜欢
  • 2017-09-12
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 2011-10-28
  • 2011-07-06
  • 2017-11-10
  • 2012-12-03
  • 2017-04-03
相关资源
最近更新 更多