python - 为什么我的图像在 OpenCV Python 中显示为灰色?
问题描述
我正在开发一个项目,该项目由我的代码识别数独拼图的图像然后解决它组成。我现在正在研究图像识别部分。它工作得很好,直到我意识到我一直在让整个程序在 y 轴上翻转。所以我换了
dimensions = np.array([[0, 0], [width, 0], [width, height], [0, height]], dtype = "float32")
和
dimensions = np.array([[width, 0], [0, 0], [0, height], [width, height]], dtype = "float32")
这似乎改变了一切,现在我运行它,我只得到一个灰色的图像。这是我的代码。请注意,我对 opencv 相当陌生。另外,我在我的代码中画线,当我运行它时,这些线仍然出现。只是没有出现实际图像。
#Imports
import cv2 as cv
import numpy as np
import math
#Load image
img = cv.imread('sudoku_test_image.jpeg')
#Transforms perspective
def perspectiveTransform(img, corners):
def orderCornerPoints(corners):
#Corners sperated into their own points
#Index 0 = top-right
# 1 = top-left
# 2 = bottom-left
# 3 = bottom-right
#Corners to points
corners = [(corner[0][0], corner[0][1]) for corner in corners]
add = np.sum(corners)
top_l = corners[np.argmin(add)]
bottom_r = corners[np.argmax(add)]
diff = np.diff(corners, 1)
top_r = corners[np.argmin(diff)]
bottom_l = corners[np.argmax(diff)]
return (top_r, top_l, bottom_l, bottom_r)
ordered_corners = orderCornerPoints(corners)
top_r, top_l, bottom_l, bottom_r = ordered_corners
#Find width of new image (Using distance formula)
width_A = np.sqrt(((bottom_r[0] - bottom_l[0]) ** 2) + ((bottom_r[1] - bottom_l[1]) ** 2))
width_B = np.sqrt(((top_r[0] - top_l[0]) ** 2) + ((top_r[1] - top_l[1]) ** 2))
width = max(int(width_A), int(width_B))
#Find height of new image (Using distance formula)
height_A = np.sqrt(((top_r[0] - bottom_r[0]) ** 2) + ((top_r[1] - bottom_r[1]) ** 2))
height_B = np.sqrt(((top_l[0] - bottom_l[0]) ** 2) + ((top_l[1] - bottom_l[1]) ** 2))
height = max(int(height_A), int(height_B))
#Make top down view
#Order: top-right, top-left, bottom-left, bottom-right
dimensions = np.array([[width, 0], [0, 0], [0, height], [width, height]], dtype = "float32")
#Make ordered_corners var numpy format
ordered_corners = np.array(ordered_corners, dtype = 'float32')
#Transform the perspective
m = cv.getPerspectiveTransform(ordered_corners, dimensions)
return cv.warpPerspective(img, m, (width, height))
#Processes image (Grayscale, median blur, adaptive threshold)
def processImage(img):
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
blur = cv.medianBlur(gray, 3)
thresh = cv.adaptiveThreshold(blur,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV,11,3)
return thresh
#Find and sort contours
img_processed = processImage(img)
cnts = cv.findContours(img_processed, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv.contourArea, reverse=True)
#Perform perspective transform
peri = cv.arcLength(cnts[0], True)
approx = cv.approxPolyDP(cnts[0], 0.01 * peri, True)
transformed = perspectiveTransform(img, approx)
#Draw lines
height = transformed.shape[0]
width = transformed.shape[1]
#for vertical lines
line_x = 0
x_increment_val = round((1/9) * width)
#for horizontal lines
line_y = 0
y_increment_val = round((1/9) * height)
#vertical lines
for i in range(10):
cv.line(transformed, (line_x, 0), (line_x, height), (0, 0, 255), 1)
line_x += x_increment_val
#horizontal lines
for i in range(10):
cv.line(transformed, (0, line_y), (width, line_y), (0, 0, 255), 1)
line_y += y_increment_val
#Show image
cv.imshow('Sudoku', transformed)
cv.waitKey(0)
cv.destroyAllWindows()
解决方案
似乎输入角点计算错误。在您的perspectiveTransform
函数中,您有以下代码段,显然计算了数独谜题的四个角:
# Corners to points
corners = [(corner[0][0], corner[0][1]) for corner in corners]
add = np.sum(corners)
top_l = corners[np.argmin(add)]
bottom_r = corners[np.argmax(add)]
diff = np.diff(corners, 1)
top_r = corners[np.argmin(diff)]
bottom_l = corners[np.argmax(diff)]
return (top_r, top_l, bottom_l, bottom_r)
检查(top_r, top_l, bottom_l, bottom_r)
元组。那些坐标是错误的,我不知道你在计算之后在做什么,corners
但是top_l
,和计算肯定有问题。如果您像这样对元组进行硬编码:bottom_r
top_r
bottom_l
ordered_corners = [(697, 99), (108, 121), (52, 730), (735, 730)] # orderCornerPoints(corners)
要通过拼图的实际角落(从右上角开始,逆时针),那么您的转换是正确的:
建议:当存在程序错误时,使用调试器,逐步调试,检查变量的实际值和中间计算。
推荐阅读
- jenkins - Jenkins 不显示 HTML 中的样式,尽管它们在 CSP 中是允许的
- appium - 有没有更好的方法在 Android 上使用 Appium 来获取元素值?
- unity3d - 如何将一个对象移动到另一个对象中心?
- hyperledger-fabric - 在清除超级账本上的私有数据时违反不可变性
- json - 从 psycopg2 和 python3 插入数据库时,Json-data 为空且没有任何错误
- csv - 如何在 VS Code 中对匹配的代码使用自定义突出显示?
- mysql - 带有一个子查询的 MYSQL 更新
- c - Makefile 构建
- python-3.x - 无法分配给运算符。python中if语句中的多个分配
- hibernate - 如何填写审计可扩展实体映射超类中的审计字段?