图像像素点颜色获取+保存图片代码
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。
#include #include #include #include #include #pragma comment(lib, "gdiplus.lib") using namespace std; using namespace Gdiplus; int GetEncoderClsid(const WCHAR* format, LSID* pClsid) { UINT num = 0; UINT size = 0; ImageCodecInfo* pImageCodecInfo = NULL; GetImageEncodersSize(&num, &size); if(size == 0) return -1; pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); if(pImageCodecInfo == NULL) return -1; GetImageEncoders(num, size, pImageCodecInfo); for(UINT j = 0; j < num; ++j) { if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 ) { *pClsid = pImageCodecInfo[j].Clsid; free(pImageCodecInfo); return j; } } free(pImageCodecInfo); return -1; } int main() { GdiplusStartupInput gdiplusstartupinput; ULONG_PTR gdiplustoken; GdiplusStartup(&gdiplustoken, &gdiplusstartupinput, nullptr); wstring infilename(L"shadow.JPG");//需要读取的图片 string outfilename("color.txt");//保存结果 //读图片 Bitmap* bmp = new Bitmap(infilename.c_str()); UINT height = bmp->GetHeight(); UINT width = bmp->GetWidth(); cout << "width " << width << ", height " << height << endl; Color color; ofstream fout(outfilename.c_str()); for (int y = 0; y < height; y++)//height for (int x = 0; x < width; x++)//width { int grey;//灰度值 bmp->GetPixel(x, y, &color); grey=((int)color.GetRed()*30+(int)color.GetGreen()*59+(int)color.GetBlue()*11)/100; //灰度值计算 fout << x << ";" << y << ";" < << (int)color.GetRed() << ";" << (int)color.GetGreen() << ";"
<< (int)color.GetBlue() << ";" <
} CLSID pngClsid; GetEncoderClsid(L"image/jpeg", &pngClsid); bmp->Save(L"change.jpg", &pngClsid); //保存图片 fout.close();
delete bmp;
GdiplusShutdown(gdiplustoken); return 0; }
本文来源:https://www.wddqw.com/doc/50cd97b29b6648d7c1c746f1.html