简体   繁体   English

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

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

I am trying to use an adaptive threshold on a 5 MB image whose Pixel is around 4017 x 3007我正在尝试在 5 MB 图像上使用自适应阈值,其像素约为 4017 x 3007

While using the simple code of threshold as mentioned below:使用下面提到的阈值的简单代码时:

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

the image displayed by OpenCV is not correct and it displayed only the top left side of an image not the whole image OpenCV 显示的图像不正确,它只显示图像的左上角而不是整个图像

But the same thing while using with matploatlib using below code:但是使用以下代码与 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()

I am able to set the threshold also but when it comes to an adaptive threshold to use with the image then the error comes up like this:我也可以设置阈值,但是当涉及到与图像一起使用的自适应阈值时,错误会像这样出现:

    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'

Any suggestion on this will be very helpful对此的任何建议都会非常有帮助

Your image is too big to be shown fully, only part of it is shown, you need to downscale your output 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)

before you show them.你向他们展示之前

You used a wrong image for the 'original' titles window - I fixed that as well:您为'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()

Your 2nd code block feeds a wrong image format into the function, hence the assertion exception:您的第二个代码块将错误的图像格式提供给函数,因此出现断言异常:

error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'cv::adaptiveThreshold'错误:(-215:断言失败)函数“cv::adaptiveThreshold”中的 src.type() == CV_8UC1

Your input to it has to conform to be CV_8UC1 ... did you check if you provide the correct inputs?您对它的输入必须符合 CV_8UC1 ...您是否检查过是否提供了正确的输入? You need to input a cv2.cvtColor(image , cv2.COLOR_BGR2GRAY) version of your image.您需要输入图像的 cv2.cvtColor(image , cv2.COLOR_BGR2GRAY) 版本。

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

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