放大的PPM图像(无过滤)
Upscaled PPM image (no filtering)
我的问题是我的PPM图像太小,当我想在窗口中显示它时(没有缩放功能,我必须将图像保存在磁盘上),我只使用一个简单的函数来提升像素(或者只是将其多次写入文件)。但在这之后,图像变得非常混乱(如屏幕截图所示),分辨率错误。在过去的4个小时里,我尝试了很多方法,但都无济于事。有什么解决办法吗?还是我只是错过了一些显而易见的事情,或者做错了很多事情?我在C++cli Windows窗体(VS)中使用PictureBox,但在图像查看器(WinExplorer、IrfanView)中显示的内容相同。
这是主代码,大小是尺寸(图像是正方形),颜色只是表示它是否想是彩色的,像素化放大了像素。此外,我在这里提供完整的数字代码
int sizemult = size;
if (color)
sizemult *= 3; //3 values of RGB
std::fstream file;
file.open("temp.ppm", std::ios::trunc | std::ios::out);
if (color)
file << "P3" << std::endl;
else
file << "P2" << std::endl;
file << size*pixelsize << " " << size*pixelsize << std::endl;
file << 256 << std::endl;
for (auto i = 0; i < size; i++) {
for (auto h = 0; h < pixelsize; h++) {
for (auto k = 0; k < size*sizemult; k++) {
for (auto j = 0; j < pixelsize; j++) {
file << table[i*size+k] << " ";
}
}
file << std::endl;
}
}
file.close();
}
结果
显然我的问题出在for循环中。这是经过更正的代码,增加了颜色兼容性。
int sizemult = size;
if (color)
sizemult *= 3;
std::default_random_engine e1(r());
std::uniform_real_distribution <double> dist(0, 256);
table = (double*)calloc(size*sizemult, sizeof(double));
for (auto j = 0; j < size*sizemult ; j++) {
table[j] = dist(e1);
//table[j] = rand() % 256;
}
std::fstream file;
file.open("temp.ppm", std::ios::trunc | std::ios::out);
if (color)
file << "P3" << std::endl;
else
file << "P2" << std::endl;
file << size*pixelsize << " " << size*pixelsize << std::endl;
file << 256 << std::endl;
for (auto i = 0; i < size; i++) {
for (auto h = 0; h < pixelsize; h++) {
for (auto k = 0; k < sizemult; k++) {
for (auto j = 0; j < pixelsize; j++) {
if (color) {
file << (int)table[i*size + k] << " ";
file << (int)table[i*size + k+1] << " ";
file << (int)table[i*size + k+2] << " ";
}
else
file << (int)table[i*size+k] << " ";
}
if (color)
k += 2;
}
file << std::endl;
}
}
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 平均图像时图像损坏
- 在C++中使用GDAL可以将图像的像素坐标转换为lat,long吗
- C++ 图像过滤算法
- 幼稚的过滤返回错误的图像
- 图像过滤不正确
- 放大的PPM图像(无过滤)
- OPENCV-直接用指针访问过滤图像,并用内核矩阵过滤
- 提高平均图像过滤速度的方法
- 过滤 1bpp 图像
- 如何过滤图像中给定宽度的线条
- 如何在OpenCV中过滤具有邻域主色的图像
- 初学者对图像过滤的尝试
- 在c++中加速图像过滤
- 为什么我过滤后的图像(应该是二进制的)仍然包含彩色部分?(C++中的OpenCV)
- 创建拉普拉斯矩阵,过滤图像
- c++, opencv:在过滤操作中对源和目标图像使用相同的Mat是否安全?