简体   繁体   English

文本边缘锯齿形效果去除(或找到图像区域的主色)

[英]Text edge zigzag effect removal (OR finding the dominant color for a image region)

My goal is to draw the text bounding boxes for the following image.我的目标是为下图绘制文本边界框。 Since the two regions are colored differently, so this should be easy.由于这两个区域的颜色不同,所以这应该很容易。 I just need to select the pixels that match a certain color values to filter out the other text region and run a convex hull detection.我只需要 select 匹配某个颜色值的像素来过滤掉其他文本区域并运行凸包检测。

在此处输入图像描述

However, when I zoom in the image, I notice that the text regions has the zig-zag effect on the edges, so I'm not able to easily find the two color values (for the blue and green) from the above image.但是,当我放大图像时,我注意到文本区域的边缘具有锯齿形效果,因此我无法从上面的图像中轻松找到两个颜色值(蓝色和绿色)。

I wonder is there a way to remove the zig-zag effect to make sure each phrase is colored consistently?我想知道有没有办法消除之字形效果以确保每个短语的颜色一致? Or is there a way to determine the dominant color for each text region?或者有没有办法确定每个文本区域的主色?

在此处输入图像描述

The anti-aliasing causes the color to become lighter (or darker if against a black background) so you can think of the color as being affected by light.抗锯齿使颜色变浅(如果在黑色背景下则变深),因此您可以将颜色视为受光的影响。 In that case, we can use light-invariant color spaces to extract the colors.在这种情况下,我们可以使用光不变颜色空间来提取 colors。

So first convert to hsv since it is a light invariant colorspace.所以首先转换为 hsv,因为它是一个光不变的色彩空间。 Since the background can be either black or white, we will filter out them out (if the bg is always white and the text can be black you would need to change the filtering to allow for that).由于背景可以是黑色或白色,我们将过滤掉它们(如果背景始终是白色并且文本可以是黑色,则需要更改过滤以允许这样做)。

I took the saturation as less than 80 as that will encompass white black and gray since they are the only colors with low saturation.我将饱和度设为小于 80,因为它将包含白色黑色和灰色,因为它们是唯一具有低饱和度的 colors。 (your image is not perfectly white, its 238 instead of 255 maybe due to jpg compression) (您的图像不是完全白色,它的 238 而不是 255 可能是由于 jpg 压缩)

Since we found all the black, white and gray, the rest of the image are our main colors, so i took the inverse mask of the filter, then to make the colors uniform and unaffected by light, set the Saturation and Value of the colors to 255, that way the only difference between all the colors will be the hue. Since we found all the black, white and gray, the rest of the image are our main colors, so i took the inverse mask of the filter, then to make the colors uniform and unaffected by light, set the Saturation and Value of the colors到 255,这样所有 colors 之间的唯一区别就是色调。 I also set bg pixels to 0 to make it easier for finding contours but thats not necissary我还将 bg 像素设置为 0,以便更容易找到轮廓,但这不是必需的

After this you can use whatever method you want to get the different groups of colors, I just did a quick histogram for the hue values and got 3 peaks but 2 were close together so they can be bundled together as 1. You can maybe use peak finding to try to find the peaks.在此之后,您可以使用任何方法来获取 colors 的不同组,我只是对色调值做了一个快速直方图,得到了 3 个峰值,但 2 个峰值很接近,因此可以将它们捆绑在一起为 1。您可以使用峰值find 试图找到峰值。 There might be better methods of finding the color groups but this is what i just thought of quickly.可能有更好的方法来查找颜色组,但这是我很快想到的。

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = hsv[:,:,1] < 80 # for white, gray & black
hsv[mask] = 0 # set bg pixels to 0
hsv[~mask,1:] = 255 # set fg pixels saturation and value to 255 for uniformity

colors = hsv[~mask]
z = np.bincount(colors[:,0])
print(z)

bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('bgr', bgr)

在此处输入图像描述

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

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