OpenCV写入保存黑色png [英] OpenCV imwrite saving black png

查看:1311
本文介绍了OpenCV写入保存黑色png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C ++程序,将平面场应用于图像。它应该加载一个图像,并将其与另一个加载的图像,然后将其导出为一个PNG。我的程序正确编译,但只产生全黑图像。

I'm trying to write a C++ program that applies a flatfield to an image. It's supposed to load an image and divide it by another loaded image, then export it as a PNG. My program compiles correctly, but only produces an all-black image.

#include <climits>
#include <stdio.h>
#include <string>
#include <opencv/cv.h>
#include <opencv/highgui.h>

using namespace cv;
using namespace std;

int main( int argc, char *argv[] )
 {
     const char *flat_file = argv[1];
     const char *img_file = argv[2];
     const char *out_file = argv[3];

     Mat flat_img;
     flat_img = imread( flat_file, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH );   
     Mat image;
     image = imread( img_file, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH );

     GaussianBlur( flat_img, flat_img, cv::Size(3,3), 0, 0, BORDER_REPLICATE );
     divide( image, flat_img, image, 1.0, -1 );
     imwrite( out_file, image );

 return 0;
 }

所有输入文件都是16位TIFF。目前,生成的输出文件是一个具有正确像素尺寸但是完全黑色的16位PNG。

All input files are 16-bit TIFFs. Currently, the resulting output file is a 16-bit PNG that has the correct pixel dimensions, but is completely black.

我对C ++和OpenCV都很新,所以我不完全确定我错过了什么。 此主题似乎表明可能浮点值是原因。我很好转换出浮点,但我需要保持我的源的16位性质。我试过在divide命令后添加一行 image.convertTo(image,CV_16U); ,同时 CV_16U CV_8U ,并且都不起作用。

I'm very new to both C++ and OpenCV, so I'm not entirely sure what I'm missing. This thread seems to suggest that maybe floating point values are the cause. I'm fine with converting out of floating point, but I need to maintain the 16-bit nature of my source. I've tried adding a line image.convertTo( image, CV_16U ); after the divide command, with both CV_16U and CV_8U and neither works.

非常感谢任何和所有帮助。

Any and all help is greatly appreciated!

编辑

根据下面的建议,我试着添加一些imshow命令来实际测试opencv是否正确处理。看来,一切工作正常,直到我的分数命令。

Per suggestions below, I tried adding some imshow commands to actually test that opencv was processing things correctly. It seems that everything works fine until my divide command. After that, my image array just ends up being blank.

推荐答案

原来是我的divide命令。我一直把我的代码放在一个同事写的类似应用程序上,所以我去看看他们是怎么做的。我不是100%肯定这是完成,但它的工作原理,当我改变我的分界为:

Turns out it was indeed my divide command. I had been basing my code on a similar application written by a coworker, so I went and looked at how they had done the division. I'm not 100% sure what this is accomplishing, but it works when I change my divide to this:

divide(image,flat_img, image,image.depth()== 16?UCHAR_MAX:USHRT_MAX);

只是进行预测,条件是设置标量我的数据基于图像的深度。我还有一个留下的问题是什么与我有和之间的区别(image,flat_img,image,image.depth()== 8?UCHAR_MAX:USHRT_MAX); 将会。两者产生看起来是相同的图像。大多数情况下,我想更好地了解情况。

Just making a prediction, that conditional is setting the scalar for my data based on the depth of the image. One lingering question I still have is what the difference between what I have and divide( image, flat_img, image, image.depth() == 8 ? UCHAR_MAX : USHRT_MAX ); would be. Both produce what appears to be the same image. Mostly, I'm trying to understand what's happening a bit better.

这篇关于OpenCV写入保存黑色png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆