當前位置:
首頁 >
常用图像格式(PNG,JPG)到SGI图像格式(RGB,BW)的转换
發布時間:2023/11/27
30
豆豆
生活随笔
收集整理的這篇文章主要介紹了
常用图像格式(PNG,JPG)到SGI图像格式(RGB,BW)的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
常用圖像格式(PNG,JPG)到SGI圖像格式(RGB,BW)的轉換
網站鏈接
根據該網站的描述和給的相應例子,自己改寫了一個把png,jpg等常用格式的圖片轉化為rgb,bw格式的代碼。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
\\待轉換的圖像長寬
#define IXSIZE (512)
#define IYSIZE (512)
#include<opencv.hpp>
using namespace cv;void putbyte(FILE *outf, unsigned char val)
{unsigned char buf[1];buf[0] = val;fwrite(buf, 1, 1, outf);
}void putshort(FILE *outf, unsigned short val)
{unsigned char buf[2];buf[0] = (val >> 8);buf[1] = (val >> 0);fwrite(buf, 2, 1, outf);
}static int putlong(FILE *outf, unsigned long val)
{unsigned char buf[4];buf[0] = (val >> 24);buf[1] = (val >> 16);buf[2] = (val >> 8);buf[3] = (val >> 0);return fwrite(buf, 4, 1, outf);
}int main()
{printf("%d\n", sizeof(unsigned));Mat img, img1;Mat dst;img = imread("./scene_dense_mesh_refine_texture1.jpg");img1 = imread("./test.jpg");printf("%d\n", img.rows);printf("%d\n", img.cols);resize(img, dst, Size(256, 256), (0, 0), (0, 0), INTER_LINEAR);imwrite("./test2.png", dst);//imwrite("./test.jpg", dst);printf("%u %u %u\n", img.at<Vec3b>(255, 0)[2], img.at<Vec3b>(255, 0)[1], img.at<Vec3b>(255, 0)[0]);printf("%u %u %u\n", img.at<Vec3b>(0, 255)[2], img.at<Vec3b>(0, 255)[1], img.at<Vec3b>(0, 255)[0]);imshow("img", img);waitKey(0);FILE *of;char iname[80];unsigned char outbuf[IXSIZE];int i, x, y;of = fopen("example.rgb", "w");if (!of) {fprintf(stderr, "sgiimage: can't open output file\n");return -1;}putshort(of, 474); /* MAGIC */putbyte(of, 0); /* STORAGE is VERBATIM */putbyte(of, 1); /* BPC is 1 */putshort(of, 3); /* DIMENSION is 2 */putshort(of, IXSIZE); /* XSIZE */putshort(of, IYSIZE); /* YSIZE */putshort(of, 3); /* ZSIZE */putlong(of, 0); /* PIXMIN is 0 */putlong(of, 255); /* PIXMAX is 255 */for (i = 0; i<4; i++) /* DUMMY 4 bytes */putbyte(of, 1);//strcpy(iname, "11111111111111111111111111111111111111111111111111111111111111111111111111111111");fwrite(iname, 80, 1, of); /* IMAGENAME */putlong(of, 0); /* COLORMAP is 0 */for (i = 0; i<404; i++) /* DUMMY 404 bytes */putbyte(of, 0);for (int z = 0; z<3; z++){for (y = 0; y<IYSIZE; y++) {for (x = 0; x < IXSIZE; x++){Vec3b vec3 = img.at<Vec3b>(IXSIZE-1-y, x);if (z == 0){if (vec3[2] == 10)vec3[2] = vec3[2] - 1;outbuf[x] = vec3[2];if(sizeof(vec3[2])!=1)printf("%d\n", sizeof(vec3[2]));if (vec3[2] > 255 || vec3[2] < 0)printf("hrer\n");}if (z == 1){//if (x > 128 && y < 128)// outbuf[x] = 255;//elseif (vec3[1] == 10)vec3[1] = vec3[1] - 1;outbuf[x] = vec3[1];}if (z == 2){//if (x < 128 && y>128)//{// outbuf[x] = 255;// }//elseif (vec3[0] == 10)vec3[0] = vec3[0] - 1;outbuf[x] = vec3[0];}}fwrite(outbuf, IXSIZE, 1, of);}}printf("sunccess\n");fclose(of);system("pause");
}
寫入像素10會有意想不到的錯誤,所以把像素10都改為了9
總結
以上是生活随笔為你收集整理的常用图像格式(PNG,JPG)到SGI图像格式(RGB,BW)的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: X战警整个系列按故事发展情节排列观看顺序
- 下一篇: 编译-链接-运行-环境配置各种error