python - 为什么我的图像显示为灰色方 block ?

标签 python image opencv

我正在用 Python 为我的图像处理类编写一个脚本,它应该读取图像的目录,显示它们,然后我最终会添加额外的代码来对这些图像执行 Otsu 阈值处理。我可以获得引用图像以正确显示以包括 Otsu 阈值;但是,当我尝试显示目录​​中的剩余图像时遇到了麻烦。我不确定是否从目录中正确读取了我的图像,因为我试图将它们存储在一个数组中;但是,我可以看到输出窗口显示的灰色方 block 对应于实际图像分辨率的尺寸,这表明它们至少部分被正确读取。

我已经尝试隔离脚本来加载图像并将它们显示到一个单独的文件中并运行它。我担心我的示例图像(包括黑白二值化)的成功处理会以某种方式影响我以后的图像显示。事实并非如此,因为运行单独的脚本会产生相同的灰色方 block 输出。

****更新****

我已经设法调整以下脚本(尚未更新)以几乎正确运行。通过直接为每个文件写入完整的文件路径,我可以获得正确显示的输出。将图像加载到数组中似乎存在一些问题,我能说的最好; future 测试的一个潜在解决方法是将文件位置作为字符串数组导入,并实现它与直接将图像加载到数组中。

import cv2 as cv
import numpy as np
from PIL import Image
import glob
from matplotlib import pyplot as plot
import time

image=cv.imread('Fig ref.jpg')

image2=cv.cvtColor(image, cv.COLOR_RGB2GRAY)
cv.imshow('Image', image)
# global thresholding
ret1,th1 = cv.threshold(image2,127,255,cv.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv.threshold(image2,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(image2,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# plot all the images and their histograms
images = [image2, 0, th1,
          image2, 0, th2,
          blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding     (v=127)',
      'Original Noisy Image','Histogram',"Otsu's Thresholding",
      'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in range(3):
    plot.subplot(3,3,i*3+1),plot.imshow(images[i*3],'gray')
    plot.title(titles[i*3]), plot.xticks([]), plot.yticks([])
    plot.subplot(3,3,i*3+2),plot.hist(images[i*3].ravel(),256)
    plot.title(titles[i*3+1]), plot.xticks([]), plot.yticks([])
    plot.subplot(3,3,i*3+3),plot.imshow(images[i*3+2],'gray')
    plot.title(titles[i*3+2]), plot.xticks([]), plot.yticks([])
plot.show()

imageFolderPath = 'D:\Google Drive\Engineering\Senior Year\Image     processing\Image processing group work'
imagePath = glob.glob(imageFolderPath + '/*.JPG') 

im_array = np.array( [np.array(Image.open(img).convert('RGB')) for img in imagePath] )
temp=cv.imread("D:\Google Drive\Engineering\Senior Year\Image processing\Image processing group work\Fig ref.jpg")


cv.imshow('image', temp)
time.sleep(15)

for i in range(9):
    cv.imshow('Image', im_array[i])


    time.sleep(2)

最佳答案

plot.subplot(3,3,i*3+3),plot.imshow(images[i*3+2],'gray'):第二个参数表示您使用 gray 彩色图。摆脱它,你会得到彩色显示器。

关于python - 为什么我的图像显示为灰色方 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55602687/

相关文章:

python - 带有日期用户输入的 Python 中的 Oracle SQL 查询

javascript - 根据单元格的值将 CSS 生成的图像分配给表格中的单元格

opencv - 带有opencv的多元随机森林

Android Coverflow OnClickListener

image - Firebug 显示图像下载两次

c++ - 在 ubuntu 中使用 tesseract

python - opencv:如何将所有黑色像素转换为透明并将其保存到png文件

python - 使用 pyinstaller 编译时 "except"期间的 mysql.connector 错误?

python - 通过列中的标签列表对 Pandas 数据框行进行分组的有效方法

python - 不小心安装了python 3.5 vs. python3.5 : Is this bad?