【问题标题】:How to remove small colored pixels from image using opencv如何使用opencv从图像中删除小的彩色像素
【发布时间】:2020-01-31 23:22:33
【问题描述】:

我正在处理一个项目,在该项目中我必须处理图像并获取彩色线条的 rgb 值,如图所示。我通过从左到右逐行处理图像来检测线条并获取值。 但是在整行之前会出现任何彩色像素。那么我的算法可能会为我产生错误的输出。

这是原文

这是运行我的代码后的图像,其中一些像素位于左行之前和之后:

我实现了像中值过滤器这样的过滤器,它可以去除小像素和噪声,但也会改变颜色值。所以过滤器对我来说毫无用处。我想要另一种方法来做到这一点。

这是我的代码:

import cv2
import numpy as np


def image_processing(img):
    msg = ''
    try:
        img = cv2.imread(img)
        # img = cv2.resize(img, (650, 120), interpolation=cv2.INTER_AREA)
        img_hsv = cv2.cvtColor(255 - img, cv2.COLOR_BGR2HSV)
    except Exception as e:
        msg += 'Unable to read image.'
    else:
        lower_red = np.array([40, 0, 0])  # lower range for red values
        upper_red = np.array([95, 255, 255])  # upper range for red values

        # mask on red color lines to find them
        mask = cv2.inRange(img_hsv, lower_red, upper_red)
        # original image with just red color pixels all other pixels will be set to 0(black)
        color_detected_img = cv2.bitwise_and(img, img, mask=mask)

        # finding the pixel where color is detected in [colored_detected_img]
        img_dimensions = color_detected_img.shape  # height & width of the image
        left_line_colors = []
        right_line_colors = []
        y_color_index = 10
        x_color_index = 0
        left_line_detected = False
        right_line_detected = False

        # getting left line values
        while x_color_index < img_dimensions[1] - 1:
            x_color_index += 1
            if color_detected_img[y_color_index, x_color_index].all() > 0:  # checking if color values are not 0 (0 is black)
                left_line_detected = True
                for y in range(img_dimensions[0] - 1):
                    # print(y, x_color_index)
                    left_line_colors.append(
                    color_detected_img[y,x_color_index].tolist())
            elif left_line_detected:
                break
            else:
                continue

        #  ---- Getting final results of left line ----
        try:
            left_line_colors = [l for l in left_line_colors if (l[0] != 0 and l[1] != 0 and l[2] != 0)]
            # adding all the rgb list values together i.e if -> [[1, 2, 3], [2, 4, 1]] then sum -> [3, 6, 4]
            sum_of_left_rgb = [sum(i) for i in zip(*left_line_colors)]
            left_rgb = [int(sum_of_left_rgb[0] / len(left_line_colors)),
                    int(sum_of_left_rgb[1] / len(left_line_colors)),
                    int(sum_of_left_rgb[2] / len(left_line_colors))]
            print(left_rgb[2], left_rgb[1], left_rgb[0])
        except:
            msg += 'No left line found.'

        cv2.imshow("Cropped", color_detected_img)

        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return msg


print(image_processing('C:/Users/Rizwan/Desktop/example_strip1.jpg'))

它应该给我结果

但是我通过实现中值模糊过滤器得到这个结果图像,它改变了产生错误结果的 rgb 值。

【问题讨论】:

  • 您不能使用从中间模糊中获得的蒙版,而是用它“查询”原始图像吗?因此,您使用中值 hack 检测到正确的像素,但从原始数据中获取正确的值。
  • 好主意@kwinkunks :)
  • @kwinkunks 解决方案是不必要的复杂。执行一个打开操作就足够了

标签: python opencv


【解决方案1】:

【讨论】:

  • 不改变颜色值吗?
  • @rizwan 被称为“开放”,不,不会改变 che 颜色值。
猜你喜欢
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-11-07
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 2021-12-24
相关资源
最近更新 更多