imread and imwrite cause differences in image pixels via opencv

Question: read an image ev and then save it as ev_cp. Finally, read the saved image ev_cp. Intuitively, ev_cp should have the same value as ev pixel by pixel, but the blew code does not think so. It indicates that the saved ev_cp image has some values(pixels) different from ev.

    ev = cv2.imread(image_path)
    im_p = results_p+'1.jpg'
    if not os.path.exists(results_p):
       os.makedirs(results_p)
    cv2.imwrite(im_p, ev)
   
    #Convert 2 images to numpy arrays and compare pixel by pixel
    ev_cp = cv2.imread(im_p)
    flag = (ev == ev_cp).all()
    print(flag)

output: False

Reason:

Be careful when using JPEG as intermediate storage for image processing tasks since it is a lossy format and values may differ when you subsequently read them back.

Consider maybe using lossless PNG format for intermediate storage. Or use NetPBM PGM (greyscale) or PPM (colour) format for a particularly simple format to read and write - though be aware it cannot persist metadata, such as copyrights or EXIF data.

Change jpg tp png could fix this issue.

im_p = results_p+str(i+1)+'.png'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值