简体   繁体   English

如何使用透视变换分割 openCV 中的白色钢琴键

[英]How to Segment White Piano Keys in openCV using Perspective Transform

I have a perspective image of a piano keyboard from the left side, like this:我从左侧有一个钢琴键盘的透视图,如下所示:

钢琴透视

I manually outline the four corners of the keyboard and fed it into the openCV getPerspectiveTransform function to get a overhead view of the keyboard.我手动勾勒出键盘的四个角并将其输入 openCV getPerspectiveTransform function 以获得键盘的俯视图。 The result is this:结果是这样的:

overhead view俯视图俯视图

I've found that if I do not outline the 4 corners of the keyboard exactly, the edges of the white keys in the perspective transform output will not be exactly vertical.我发现,如果我不准确地勾勒出键盘的 4 个角,透视变换 output 中的白键边缘将不会完全垂直。

Then I want to produce an outline of all the white keys.然后我想生成所有白键的轮廓。 Since the white keys of the piano are all evenly spaced, I just divided the top view image equally into 33 parts horizontally.由于钢琴的白键都是均匀分布的,所以我只是将顶视图图像水平分成了 33 个部分。 Then I scaled the bottom edges of the outline by the ratio of the length of the bottom side to the top side of the keyboard in the original image.然后,我按照原始图像中键盘底边与顶边的长度之比来缩放轮廓的底边。 I got this output.我得到了这个 output。

在此处输入图像描述

However, the outlines of the white keys do not match the white key edges.但是,白键的轮廓与白键边缘不匹配。

How do I take advantage of the fact that the white keys are all evenly spaced to segment the white piano keys, even when the edges of the white keys are not perfectly vertical?即使白键的边缘不是完全垂直的,我如何利用白键都均匀分布来分割钢琴白键这一事实?

*I don't want to use edge detection algorithm to segment the white keys as sometimes in certain lighting conditions, the edges are not visible. *我不想使用边缘检测算法来分割白键,因为有时在某些光照条件下,边缘是不可见的。

It looks like the perspective transformation is a bit off using only 4 points.看起来仅使用 4 个点的透视变换有点偏离。 Try using a few more and use cv2.findHomography together with cv2.warpPerspective to do the job.尝试使用更多并使用 cv2.findHomography 和 cv2.warpPerspective 来完成这项工作。 Once you have a better perspective transformation, you can continue doing the rest.一旦有了更好的透视变换,就可以继续做 rest。

Code sample of using v2.findHomography together with cv2.warpPerspective can be found:可以找到使用 v2.findHomography 和 cv2.warpPerspective 的代码示例:

https://www.learnopencv.com/homography-examples-using-opencv-python-c/ https://www.learnopencv.com/homography-examples-using-opencv-python-c/

Good luck.祝你好运。

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

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