如何使用opencv从图像中删除小的彩色像素

时间:2019-10-02 16:28:08

标签: python opencv

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

这是原始的

enter image description here

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

enter image description here

我实现了像中值过滤器一样的过滤器,该过滤器除去了较小的像素和噪点,但也改变了颜色值。因此过滤器对我来说毫无用处。我想要另一种方法。

这是我的代码:

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

它应该给我结果

enter image description here

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

1 个答案:

答案 0 :(得分:0)

尝试使用侵蚀然后进行扩张Click here to know about erosion and dialtion