gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:38:49 26 4
gpt4 key购买 nike

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

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

最佳答案

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

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

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

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

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

关于python - 向 cv2.imshow() 提供大 (4017*3007) 图像不会显示整个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55915217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com