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

时间:2019-04-30 06:38:36

标签: python python-3.x opencv opencv3.0

我正在尝试对像素约为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'

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

1 个答案:

答案 0 :(得分:1)

您的图片太大,无法完全显示,只显示其中一部分,您需要缩小输出窗口:

# 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'标题窗口中使用了错误的图片-我也对此进行了修复:

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()

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

  

错误:(-215:声明失败)函数'cv :: adaptiveThreshold'中的src.type()== CV_8UC1

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