【发布时间】:2020-09-05 15:15:53
【问题描述】:
问题: 我正在使用一个包含许多看起来像这样的图像的数据集:
现在我需要将所有这些图像水平或垂直定向,以便调色板位于图像的底部或右侧。这可以通过简单地旋转图像来完成,但棘手的部分是确定哪些图像应该旋转,哪些不应该。
我尝试过的:
我认为最好的方法是检测将调色板与图像分开的白线。我决定旋转所有底部有调色板的图像,使其位于右侧。
# yes I am mixing between PIL and opencv (I like the PIL resizing more)
# resize image to be 128 by 128 pixels
img = img.resize((128, 128), PIL.Image.BILINEAR)
img = np.array(img)
# perform edge detection, not sure if these are the best parameters for Canny
edges = cv2.Canny(img, 30, 50, 3, apertureSize=3)
has_line = 0
# take numpy slice of the area where the white line usually is
# (not always exactly in the same spot which probably has to do with the way I resize my image)
for line in edges[75:80]:
# check if most of one of the lines contains white pixels
counts = np.bincount(line)
if np.argmax(counts) == 255:
has_line = True
# rotate if we found such a line
if has_line == True:
s = np.rot90(s)
一个正常工作的例子:
一个不正常工作的例子:
这可能适用于 98% 的图像,但在某些情况下,它会旋转不应旋转的图像或不旋转应旋转的图像。也许有一种更简单的方法可以做到这一点,或者可能有一种更复杂、更一致的方法?我可以手动完成,但我要处理很多图像。感谢您的帮助和/或 cmets。
以下是我的代码在测试时失败的一些图片:
【问题讨论】:
-
所有图像中彩条的数量是否始终相同?
-
是的,总是 6 种颜色
-
是否可以假设对于所有相同类型的图像,白条始终位于相同位置?
-
是的,但它们并不完全是像素完美的。我可能可以检测到图像底部是否有 5 个垂直条并这样做。
-
我这样做的方法是为白色区域(水平和垂直)创建两个蒙版,在图像和全白图像之间的蒙版区域中进行绝对差异(基本上,我只会得到带有白条的区域的差异图像),将所有像素增量相加并选择差异最小的那个。 (注意:我在这里写这个而不是作为答案,因为我现在没有时间/机会将其进一步阐述为完整的答案)
标签: python image opencv python-imaging-library