簡體   English   中英

為什么我的圖像在 OpenCV Python 中顯示為灰色?

[英]Why is my image appearing gray in 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)元組。 那些坐標是錯誤的,我不知道你在計算top_l corners后在做什么,但是top_lbottom_rtop_rbottom_l計算肯定有問題。 如果您像這樣對元組進行硬編碼:

ordered_corners = [(697, 99), (108, 121), (52, 730), (735, 730)] # orderCornerPoints(corners)

要通過拼圖的實際角(從右上角開始,逆時針方向)所在的位置,那么您的轉換是正確的:

在此處輸入圖片說明

建議:當出現程序錯誤時,請使用調試器並逐步調試以檢查變量的實際值和中間計算。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM