向 cv2.imshow() 提供大 (4017*3007) 图像不会显示整个图像

Providing big (4017*3007) image to cv2.imshow() does not display the whole image

我正在尝试对像素约为 4017 x 3007 的 5 MB 图像使用自适应阈值

使用下面提到的简单阈值代码时:

import cv2
import numpy as np

img = cv2.imread('p2.png')
#retval, threshold = cv2.threshold(img, pixel parameter below (will be black), pixel parameter above (will be white), cv2.THRESH_BINARY)
retval, threshold = cv2.threshold(img, 140 , 255, cv2.THRESH_BINARY)

cv2.imshow('threshold', threshold)

#For gray Scale
grayscaled = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled, 120 , 255, cv2.THRESH_BINARY)
cv2.imshow('threshold2', threshold2)

#Gray Scale with Gaussian for Adaptive threshold to give a clear Image
gauss = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 115,1)
cv2.imshow('gauss', gauss)

#otsu Threshold
retval2, otsu = cv2.threshold(grayscaled, 150,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('otsu', otsu)


cv2.imshow('original', threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

OpenCV显示的图像不正确,它只显示图像的左上角而不是整个图像

但是使用下面的代码与 matploatlib 一起使用时也是一样的:

import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
image = cv2.imread("p2.JPG")
ret,threshold = cv2.threshold(image,127,255,cv2.THRESH_BINARY)
th = cv2.adaptiveThreshold(grayscaled, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
plt.imshow(threshold)
plt.axis("off")
#plt.imshow(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
plt.show()

我也可以设置阈值,但是当涉及到与图像一起使用的自适应阈值时,就会出现这样的错误:

    th = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\thresh.cpp:1627: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'

对此的任何建议都会非常有帮助

你的图片太大,无法完整显示,只显示了一部分,你需要缩小你的输出windows:

# prepare windows, scale to 600x600px:
for n in ["threshold","threshold2", "gauss", "otsu","original"]:
    cv2.namedWindow(n,cv2.WINDOW_NORMAL)
    cv2.resizeWindow(n, 600,600)

你给他们看之前。

您为 'original' 标题使用了错误的图像 window - 我也修复了这个问题:

import cv2     
import numpy as np

# changed p2.png
img = cv2.imread('./big.png')  # big.png: 5000*5000 image - change it to your name again!

# prepare windows, scale to 600x600px:
for n in ["threshold","threshold2", "gauss", "otsu","original"]:
    cv2.namedWindow(n,cv2.WINDOW_NORMAL)
    cv2.resizeWindow(n, 600,600)

img = cv2.imread('p2.png')
retval, threshold = cv2.threshold(img, 140 , 255, cv2.THRESH_BINARY) 
cv2.imshow('threshold', threshold)

#For gray Scale
grayscaled = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
retval2, threshold2 = cv2.threshold(grayscaled, 120 , 255, cv2.THRESH_BINARY)
cv2.imshow('threshold2', threshold2)

#Gray Scale with Gaussian for Adaptive threshold to give a clear Image
gauss = cv2.adaptiveThreshold(grayscaled, 255, 
                              cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 115,1)
cv2.imshow('gauss', gauss)

#otsu Threshold
retval2, otsu = cv2.threshold(grayscaled, 150,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
cv2.imshow('otsu', otsu)


cv2.imshow('original', img)   # fixed here to show the original
cv2.waitKey(0)
cv2.destroyAllWindows()

您的第二个代码块将错误的图像格式输入函数,因此出现断言异常:

error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'

您的输入必须符合 CV_8UC1 ...您检查过您提供的输入是否正确吗?您需要输入图像的 cv2.cvtColor(image , cv2.COLOR_BGR2GRAY) 版本。