Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向cv2.imshow()提供大(4017*3007)图像不会显示整个图像_Python_Python 3.x_Opencv_Opencv3.0 - Fatal编程技术网

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

Python 向cv2.imshow()提供大(4017*3007)图像不会显示整个图像,python,python-3.x,opencv,opencv3.0,Python,Python 3.x,Opencv,Opencv3.0,我尝试在像素约为4017x3007的5MB图像上使用自适应阈值 在使用下面提到的简单阈值代码时: 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

我尝试在像素约为4017x3007的5MB图像上使用自适应阈值

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

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

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

错误:(-215:断言失败)函数“CV::adaptiveThreshold”中的src.type()==CV_8UC1

您对它的输入必须符合CV_8UC1。。。您是否检查了是否提供了正确的输入?您需要输入图像的cv2.CVT颜色(图像,cv2.COLOR\u BGR2GRAY)版本。

我正在跟踪此链接:()并尝试仅显示自适应阈值,但在执行此操作时,我仍然发现相同的问题。我猜问题是由于无法定义全局阈值。可能重复