使用Opencv以ppm P3 ASCII格式转换jpg图像

时间:2022-09-25 21:19:44

Using opencv imwrite function I managed to convert jpg image in ppm P6 format.

使用opencv imwrite函数我设法转换为ppm P6格式的jpg图像。

Mat image = imread(picPath);

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PXM_BINARY);
compression_params.push_back(1);

imwrite("bez.ppm", image, compression_params);

Problem is that I actually have to convert jpg image in ppm P3 ASCII format. Does anyone know how to do it? Thanks!

问题是我实际上必须转换ppm P3 ASCII格式的jpg图像。有谁知道怎么做?谢谢!

EDIT:

In the project I have the following piece of code where I check the maximum value of pixels:

在项目中,我有以下代码,我检查像素的最大值:

int maxVal; 
fscanf(in, "%d", &maxVal); 
if (maxVal != 255)
{ 
    printf("Input file error: Not a Netpbm color image with 256 levels\n");
    exit(0); 
} 

When I set parameter 0 then I get: Not a Netpbm color image with 256 levels!

当我设置参数0然后我得到:不是具有256级的Netpbm彩色图像!

When I do the conversion from jpg to ppm p3 with irfanview program works.

当我使用irfanview程序进行从jpg到ppm p3的转换时。

1 个解决方案

#1


0  

The code involved is in the file modules/imgcodecs/src/grfmt_pxm.cpp in the OpenCV source tree.

涉及的代码位于OpenCV源代码树的文件modules / imgcodecs / src / grfmt_pxm.cpp中。

It sets the internal flag isBinary like this according to the compression parameters:

它根据压缩参数设置内部标志isBinary:

for( size_t i = 0; i < params.size(); i += 2 )
   if( params[i] == CV_IMWRITE_PXM_BINARY )
      isBinary = params[i+1] != 0;

so, if you want ASCII (P3) you need to have

所以,如果你需要ASCII(P3),你需要

compression_params.push_back(0)

and have image type CV_8UC1, CV_8UC3 or CV_16UC1.

并具有图像类型CV_8UC1,CV_8UC3或CV_16UC1。

#1


0  

The code involved is in the file modules/imgcodecs/src/grfmt_pxm.cpp in the OpenCV source tree.

涉及的代码位于OpenCV源代码树的文件modules / imgcodecs / src / grfmt_pxm.cpp中。

It sets the internal flag isBinary like this according to the compression parameters:

它根据压缩参数设置内部标志isBinary:

for( size_t i = 0; i < params.size(); i += 2 )
   if( params[i] == CV_IMWRITE_PXM_BINARY )
      isBinary = params[i+1] != 0;

so, if you want ASCII (P3) you need to have

所以,如果你需要ASCII(P3),你需要

compression_params.push_back(0)

and have image type CV_8UC1, CV_8UC3 or CV_16UC1.

并具有图像类型CV_8UC1,CV_8UC3或CV_16UC1。