使用c++代码实现图像的颜色切换
Color switching of an image using C++ code
因为我对c++和图像处理很陌生,所以我在修改和添加函数到代码时遇到了问题。只需要在RGB颜色之间切换。
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "ctype.h"
#include "math.h"
class myImageData
{
private:
int mW;
int mH;
int mCH;
double * mData;
void SkipComments(FILE *fp)
{
int ch;
char line[100];
while ((ch = fgetc(fp)) != EOF && isspace(ch))
;
if (ch == '#')
{
fgets(line, sizeof(line), fp);
SkipComments(fp);
}
else
{
fseek(fp, -1, SEEK_CUR);
}
}
public:
myImageData(void)
{
this->mData = NULL;
}
~myImageData()
{
if (this->mData != NULL)
{
delete[] this->mData;
}
}
void init(int W, int H, int CH)
{
this->mW = W;
this->mH = H;
this->mCH = CH;
if (this->mData != NULL)
delete[] this->mData;
this->mData = new double[(this->mW)*(this->mH)*(this->mCH)];
}
int getWidth(void)
{
return this->mW;
}
int getHeight(void)
{
return this->mH;
}
int getCH(void)
{
return this->mCH;
}
double * getDataPtr(void)
{
return this->mData;
}
double get(int x, int y)
{
return this->mData[y*(this->mW) + x];
}
double get(int x, int y, int CH)
{
return this->mData[this->mCH * (y*(this->mW) + x) + CH];
}
void set(int x, int y, double value)
{
this->mData[y*(this->mW) + x] = value;
}
void set(int x, int y, int CH, double value)
{
this->mData[this->mCH *(y*(this->mW) + x) + CH] = value;
}
void read(const char *filename);
void save(const char *filename);
};
void myImageData::read(const char *filename)
{
FILE *file = fopen(filename, "r");
if (file == NULL){
printf("Cannot open %sn", filename);
exit(1); //abnormal termination
}
printf("Read an image from: %sn", filename);
// read ppm/pgm header
char buf[256];
char filetype[256];
int W, H, Range, CH;
fgets(buf, sizeof(buf), file);
sscanf(buf, "%s", filetype);
SkipComments(file);
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d%d", &W, &H);
SkipComments(file);
fgets(buf, sizeof(buf), file);
sscanf(buf, "%d", &Range);
//printf("Header: %s, %d, %d, %dn", filetype, W, H, Range);
SkipComments(file);
if (strcmp(filetype, "P5") == 0)
{
CH = 1;
}
else if (strcmp(filetype, "P6") == 0)
{
CH = 3;
}
else
{
printf("Unknown image typen");
exit(1); //abnormal termination
}
if (Range != 255){
printf("Invalid datan");
exit(1); //abnormal termination
}
// create myImageData class
init(W, H, CH);
// read ppm data
int datalength = this->mW * this->mH * this->mCH;
unsigned char * temp = new unsigned char[datalength];
fread(temp, sizeof(unsigned char), datalength, file);
double * ptr1 = this->mData;
unsigned char *ptr2 = temp;
for (int i = 0; i < datalength; i++){
*ptr1 = (double)*ptr2;
ptr1++;
ptr2++;
}
delete[] temp;
fclose(file);
}
void myImageData::save(const char *filename){
char filenamefull[256];
if (this->mCH == 1){
sprintf(filenamefull, "%s.pgm", filename);
}
else{
sprintf(filenamefull, "%s.ppm", filename);
}
FILE *file = fopen(filenamefull, "w");
printf("Write an image to: %s n", filenamefull);
if (this->mCH == 1){
fprintf(file, "P5n");
}
else{
fprintf(file, "P6n");
}
fprintf(file, "%d %dn", this->mW, this->mH);
fprintf(file, "255n");
int datalength = this->mW * this->mH * this->mCH;
unsigned char * temp = new unsigned char[datalength];
double * ptr1 = this->mData;
unsigned char * ptr2 = temp;
for (int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if (value > 255) value = 255;
if (value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
fwrite(temp, sizeof(unsigned char), datalength, file);
delete[] temp;
fprintf(file, "Â¥n");
fclose(file);
}
我的错误:
在函数__tmaincrtstartup中引用的未解析的外部符号_main
错误LNK1120: 1未解析的外部
首先,您没有main
函数。毫无疑问,你的代码不能正常工作。你所拥有的只是一个类来加载、保存和操作PPM图像文件。
你似乎在使用Visual Studio,所以你需要一个函数,看起来像这样:
int _tmain(int argc, _TCHAR* argv[])
{
myImageData image;
image.read("atestfile.ppm");
// do some stuff to your image
image.write("outputfile.ppm");
}
我假设你有一个PPM格式的测试图像,你可以在这里使用。
现在这是疯狂的:
double * ptr1 = this->mData;
unsigned char * ptr2 = temp;
for (int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if (value > 255) value = 255;
if (value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
您从unsigned char
中读取,因此没有必要将其填充到double
中,并且肯定没有必要检查值是否在0到255之外。你为什么要把东西放在双箱里?这毫无意义!即使您确实做了一些需要每个通道完整的双精度浮点值的事情,当您再次将所有内容限制为0-255时,也会在输出中丢弃它:
for (int i = 0; i < datalength; i++){
double value = *ptr1;
value = round(value);
if (value > 255) value = 255;
if (value < 0) value = 0;
*ptr2 = (unsigned char)value;
ptr1++;
ptr2++;
}
同样,这基本上是在薄薄的c++外表下的C。没关系,每个人都得有个起点。但是,您可以这样做,而不是使用new
来创建数组:
// read ppm data
int datalength = this->mW * this->mH * this->mCH;
// using a std::vector here means that the allocated memory will be freed
// automatically, even in the result of an error occurring.
std::vector<unsigned char> temp(datalength);
fread(&temp[0], sizeof(unsigned char), datalength, file);
我也会考虑使用iostream
类,如fstream
,而不是fread
和fopen
等。但这里不是讨论这些细节的地方。
void swapRB(myImageData& image)
{
assert(image.getCH() == 3);
for (int x = 0; x < image.getWidth())
{
for (int y = 0; x < image.getHeight())
{
double R = image.get(x, y, 0);
double G = image.get(x, y, 1);
double B = image.get(x, y, 2);
image.set(x, y, 0, B);
image.set(x, y, 2, R);
}
}
}
相关文章:
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 如何返回定义良好的内存部分?例如来自图像数据的像素的颜色值
- 如何使用OpenCV同时显示"深度"和"颜色"视频图像
- 访问和更改基于另一个图像的像素颜色-opencv c++
- 可视化 如何获取图像中像素的颜色(以C++ (sfml) 为单位)
- 同一图像中的反转文本和背景颜色
- 使用 OpenCV 更改图像的颜色
- 如何使用 openCV 更改图像每个像素的颜色
- 如何从 GIFLIB 保存图像结构中获取 RGB 颜色数据
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- 无法将图像的所有像素更改为所选颜色
- 将RGB颜色图像转换为OpenCV C 中的索引颜色图像类型
- 24位图图像和16位图图像中的RGB颜色
- RGB颜色图像直方图使用OpenCV C 进行对比度拉伸
- 如何在opencv中为图像添加边框,边框颜色必须与图像颜色相同
- 将颜色图像转换为OpenCV中的灰度问题
- 为qlineedit设置了背景图像时,如何在qlineedit中设置文本颜色
- opencv c 中的蒙版颜色图像
- 如何发现图像或多或少是均匀的w.r.t颜色(色调)
- 如何在应用颜色校正之前使输入图像线性化