matlab的imshow python中cv2.imshow及plt.imshow相关测试

在对灰度图、二值图测试显示过程中,发现matlab的imshow函数和plt.imshow函数显示的图像会出现信息缺失情况,让人误以为图像矩阵像素值发生变化。经过测试,是由于imshow函数存在像素缩放导致。可通过缩放原图,或者放大结果图像进行完整信息显示。

  • matlab: imshow
function Testimshow()
    img = imread('demoshape.png');
    if(numel(size(img)) > 2)
        img = rgb2gray(img);
    end
    %  img = imresize(img,[300,400]);   % 缩小图像
    px = img(2:end,:) - img(1:end-1,:); % 数据类型均为uint8, 负数强制为0. python中负数强制为其补数
    qy = img(:,2:end) - img(:,1:end-1);

    figure
    subplot(131)
    imshow(img)
    title('img')

    subplot(132)
    imshow(px)
    title('px')
    subplot(133)
    imshow(qy)
    title('qy')

end

在这里插入图片描述
因为图片过大,导致有些信息无法显示,通过缩小原图,或者直接对结果进行放大可以看到另一条边缘:
在这里插入图片描述

  • plt.imshow 同样存在该问题
def TestCV_plt():
    img = cv2.imread('./imgs/others/img/demoshape.png',0)

    H,W = img.shape
	# img = cv2.imresize(img,[400,300])  #  [列数,行数]
    imglight = img[1:H, :]
    imgdark = img[0:H - 1, :]

    px = CalcImgDiff(imglight,imgdark,0)

    imglight = img[:,1:W]
    imgdark = img[:,0:W-1]

    qy = CalcImgDiff(imglight, imgdark,0)

    cv2.imshow('px',px)
    cv2.imshow('qy',qy)

    cv2.waitKey(0)

    plt.figure()
    # plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.05, hspace=0.05)
    # plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.1, hspace=0.05)
    plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.05, hspace=0.1)

    plt.subplot(221)
    plt.imshow(px,'gray')

    plt.subplot(222)
    plt.imshow(qy, 'gray')

    plt.subplot(223)
    plt.imshow(px, 'gray',vmin=0,vmax=255)

    plt.subplot(224)
    plt.imshow(qy, 'gray',vmin=0,vmax=255)

    plt.show()

在这里插入图片描述
在这里插入图片描述
plt.imshow会自动对灰度图进行图像增强处理, 可以通过vmin和vmax调节线性灰度变换效果.
类似matlab中操作,通过缩小原图,或者改变子图间距来调整显示结果。

  • cv2.imshow 不存在该问题
    cv2.imshow对图像进行原图显示,不会进行像素缩放,所以显示完整可靠。
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值