将jet图像改为灰度图
来源:互联网 发布:34401a编程手册 编辑:程序博客网 时间:2024/06/18 01:56
jet是一个256位颜色索引,用matlab可以把灰度图变成jet图片,但是怎么反过来我在matlab上没找到,所以只好自己另写程序了。
以下是jet.txt,是jet的颜色索引。
0.000000 0.000000 0.5156250.000000 0.000000 0.5312500.000000 0.000000 0.5468750.000000 0.000000 0.5625000.000000 0.000000 0.5781250.000000 0.000000 0.5937500.000000 0.000000 0.6093750.000000 0.000000 0.6250000.000000 0.000000 0.6406250.000000 0.000000 0.6562500.000000 0.000000 0.6718750.000000 0.000000 0.6875000.000000 0.000000 0.7031250.000000 0.000000 0.7187500.000000 0.000000 0.7343750.000000 0.000000 0.7500000.000000 0.000000 0.7656250.000000 0.000000 0.7812500.000000 0.000000 0.7968750.000000 0.000000 0.8125000.000000 0.000000 0.8281250.000000 0.000000 0.8437500.000000 0.000000 0.8593750.000000 0.000000 0.8750000.000000 0.000000 0.8906250.000000 0.000000 0.9062500.000000 0.000000 0.9218750.000000 0.000000 0.9375000.000000 0.000000 0.9531250.000000 0.000000 0.9687500.000000 0.000000 0.9843750.000000 0.000000 1.0000000.000000 0.015625 1.0000000.000000 0.031250 1.0000000.000000 0.046875 1.0000000.000000 0.062500 1.0000000.000000 0.078125 1.0000000.000000 0.093750 1.0000000.000000 0.109375 1.0000000.000000 0.125000 1.0000000.000000 0.140625 1.0000000.000000 0.156250 1.0000000.000000 0.171875 1.0000000.000000 0.187500 1.0000000.000000 0.203125 1.0000000.000000 0.218750 1.0000000.000000 0.234375 1.0000000.000000 0.250000 1.0000000.000000 0.265625 1.0000000.000000 0.281250 1.0000000.000000 0.296875 1.0000000.000000 0.312500 1.0000000.000000 0.328125 1.0000000.000000 0.343750 1.0000000.000000 0.359375 1.0000000.000000 0.375000 1.0000000.000000 0.390625 1.0000000.000000 0.406250 1.0000000.000000 0.421875 1.0000000.000000 0.437500 1.0000000.000000 0.453125 1.0000000.000000 0.468750 1.0000000.000000 0.484375 1.0000000.000000 0.500000 1.0000000.000000 0.515625 1.0000000.000000 0.531250 1.0000000.000000 0.546875 1.0000000.000000 0.562500 1.0000000.000000 0.578125 1.0000000.000000 0.593750 1.0000000.000000 0.609375 1.0000000.000000 0.625000 1.0000000.000000 0.640625 1.0000000.000000 0.656250 1.0000000.000000 0.671875 1.0000000.000000 0.687500 1.0000000.000000 0.703125 1.0000000.000000 0.718750 1.0000000.000000 0.734375 1.0000000.000000 0.750000 1.0000000.000000 0.765625 1.0000000.000000 0.781250 1.0000000.000000 0.796875 1.0000000.000000 0.812500 1.0000000.000000 0.828125 1.0000000.000000 0.843750 1.0000000.000000 0.859375 1.0000000.000000 0.875000 1.0000000.000000 0.890625 1.0000000.000000 0.906250 1.0000000.000000 0.921875 1.0000000.000000 0.937500 1.0000000.000000 0.953125 1.0000000.000000 0.968750 1.0000000.000000 0.984375 1.0000000.000000 1.000000 1.0000000.015625 1.000000 0.9843750.031250 1.000000 0.9687500.046875 1.000000 0.9531250.062500 1.000000 0.9375000.078125 1.000000 0.9218750.093750 1.000000 0.9062500.109375 1.000000 0.8906250.125000 1.000000 0.8750000.140625 1.000000 0.8593750.156250 1.000000 0.8437500.171875 1.000000 0.8281250.187500 1.000000 0.8125000.203125 1.000000 0.7968750.218750 1.000000 0.7812500.234375 1.000000 0.7656250.250000 1.000000 0.7500000.265625 1.000000 0.7343750.281250 1.000000 0.7187500.296875 1.000000 0.7031250.312500 1.000000 0.6875000.328125 1.000000 0.6718750.343750 1.000000 0.6562500.359375 1.000000 0.6406250.375000 1.000000 0.6250000.390625 1.000000 0.6093750.406250 1.000000 0.5937500.421875 1.000000 0.5781250.437500 1.000000 0.5625000.453125 1.000000 0.5468750.468750 1.000000 0.5312500.484375 1.000000 0.5156250.500000 1.000000 0.5000000.515625 1.000000 0.4843750.531250 1.000000 0.4687500.546875 1.000000 0.4531250.562500 1.000000 0.4375000.578125 1.000000 0.4218750.593750 1.000000 0.4062500.609375 1.000000 0.3906250.625000 1.000000 0.3750000.640625 1.000000 0.3593750.656250 1.000000 0.3437500.671875 1.000000 0.3281250.687500 1.000000 0.3125000.703125 1.000000 0.2968750.718750 1.000000 0.2812500.734375 1.000000 0.2656250.750000 1.000000 0.2500000.765625 1.000000 0.2343750.781250 1.000000 0.2187500.796875 1.000000 0.2031250.812500 1.000000 0.1875000.828125 1.000000 0.1718750.843750 1.000000 0.1562500.859375 1.000000 0.1406250.875000 1.000000 0.1250000.890625 1.000000 0.1093750.906250 1.000000 0.0937500.921875 1.000000 0.0781250.937500 1.000000 0.0625000.953125 1.000000 0.0468750.968750 1.000000 0.0312500.984375 1.000000 0.0156251.000000 1.000000 0.0000001.000000 0.984375 0.0000001.000000 0.968750 0.0000001.000000 0.953125 0.0000001.000000 0.937500 0.0000001.000000 0.921875 0.0000001.000000 0.906250 0.0000001.000000 0.890625 0.0000001.000000 0.875000 0.0000001.000000 0.859375 0.0000001.000000 0.843750 0.0000001.000000 0.828125 0.0000001.000000 0.812500 0.0000001.000000 0.796875 0.0000001.000000 0.781250 0.0000001.000000 0.765625 0.0000001.000000 0.750000 0.0000001.000000 0.734375 0.0000001.000000 0.718750 0.0000001.000000 0.703125 0.0000001.000000 0.687500 0.0000001.000000 0.671875 0.0000001.000000 0.656250 0.0000001.000000 0.640625 0.0000001.000000 0.625000 0.0000001.000000 0.609375 0.0000001.000000 0.593750 0.0000001.000000 0.578125 0.0000001.000000 0.562500 0.0000001.000000 0.546875 0.0000001.000000 0.531250 0.0000001.000000 0.515625 0.0000001.000000 0.500000 0.0000001.000000 0.484375 0.0000001.000000 0.468750 0.0000001.000000 0.453125 0.0000001.000000 0.437500 0.0000001.000000 0.421875 0.0000001.000000 0.406250 0.0000001.000000 0.390625 0.0000001.000000 0.375000 0.0000001.000000 0.359375 0.0000001.000000 0.343750 0.0000001.000000 0.328125 0.0000001.000000 0.312500 0.0000001.000000 0.296875 0.0000001.000000 0.281250 0.0000001.000000 0.265625 0.0000001.000000 0.250000 0.0000001.000000 0.234375 0.0000001.000000 0.218750 0.0000001.000000 0.203125 0.0000001.000000 0.187500 0.0000001.000000 0.171875 0.0000001.000000 0.156250 0.0000001.000000 0.140625 0.0000001.000000 0.125000 0.0000001.000000 0.109375 0.0000001.000000 0.093750 0.0000001.000000 0.078125 0.0000001.000000 0.062500 0.0000001.000000 0.046875 0.0000001.000000 0.031250 0.0000001.000000 0.015625 0.0000001.000000 0.000000 0.0000000.984375 0.000000 0.0000000.968750 0.000000 0.0000000.953125 0.000000 0.0000000.937500 0.000000 0.0000000.921875 0.000000 0.0000000.906250 0.000000 0.0000000.890625 0.000000 0.0000000.875000 0.000000 0.0000000.859375 0.000000 0.0000000.843750 0.000000 0.0000000.828125 0.000000 0.0000000.812500 0.000000 0.0000000.796875 0.000000 0.0000000.781250 0.000000 0.0000000.765625 0.000000 0.0000000.750000 0.000000 0.0000000.734375 0.000000 0.0000000.718750 0.000000 0.0000000.703125 0.000000 0.0000000.687500 0.000000 0.0000000.671875 0.000000 0.0000000.656250 0.000000 0.0000000.640625 0.000000 0.0000000.625000 0.000000 0.0000000.609375 0.000000 0.0000000.593750 0.000000 0.0000000.578125 0.000000 0.0000000.562500 0.000000 0.0000000.546875 0.000000 0.0000000.531250 0.000000 0.0000000.515625 0.000000 0.0000000.500000 0.000000 0.000000
代码:
//本程序的目的是改变jet到gray#include <stdlib.h>#include <stdio.h>#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;#include <iostream>using namespace std;int getgray(Mat jet, Vec3b color){double distance[256];for (int i = 0; i< 256; i++){double b = (color[0]*1.0/255-jet.at<Vec3f>(i, 0)[0])*(color[0]*1.0/255-jet.at<Vec3f>(i, 0)[0]);double g = (color[1]*1.0/255-jet.at<Vec3f>(i, 0)[1])*(color[1]*1.0/255-jet.at<Vec3f>(i, 0)[1]);double r = (color[2]*1.0/255-jet.at<Vec3f>(i, 0)[2])*(color[2]*1.0/255-jet.at<Vec3f>(i, 0)[2]);distance[i] = b+g+r;}int re = 0;for (int i = 1; i< 256; i++){if (distance[i]< distance[re]){re = i;}}return re;}int main(){//读取jetFILE *fp = fopen("jet.txt", "r");if (!fp) {cout << "cannot open file!\a"<< endl;return -1;}Mat jet = Mat(256, 1, CV_32FC3, Scalar::all(0));double r, g, b;int line = 0;char token[100];while(!feof(fp)){token[0] = NULL;fgets(token, 100, fp);//cout<<token<<endl;char tmp[9], tmp2[9], tmp3[9];for (int i = 0; i<8; i++){tmp[i] = token[i];}tmp[8] = '0';for (int i = 9; i<17; i++){tmp2[i-9] = token[i];}tmp2[8] = '0';for (int i = 18; i<26; i++){tmp3[i-18] = token[i];}tmp3[8] = '0';r = atof(tmp);g = atof(tmp2);b = atof(tmp3);//cout<<r<<"\t"<<g<<"\t"<<b<<endl;assert(line<256);jet.at<Vec3f>(line, 0) = Vec3f(b, g, r);line++;}//读取图片string name[] ={"candies", "draws", "girls", "seal", "sheep", "taxi"};for (int seg = 0; seg< name->length(); seg++){string realname = "input/"+name[seg]+".png";string outputname = "output/"+name[seg]+"_gray.png";Mat color = imread(realname);Mat gray = Mat(color.rows, color.cols, CV_8UC1, Scalar::all(0));for (int i = 0; i< color.rows; i++){cout<<"%"<<i*100/color.rows<<endl;for (int j = 0; j< color.cols; j++){gray.at<uchar>(i, j) = getgray(jet, color.at<Vec3b>(i, j));}}imshow("gray", gray);waitKey(33);imwrite(outputname, gray);}return 0;}
以下是把gray转换为obj文件(默认灰度值代表深度值)
代码:
//本程序的目的是输出obj#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;#include <iostream>#include <fstream>using namespace std;void getmtl(string name){ofstream obj("output/"+name+".mtl");obj<<"\nnewmtl material_0\n"<<"Ka 0.200000 0.200000 0.200000\n"<<"Kd 1.000000 1.000000 1.000000\n"<<"Ks 1.000000 1.000000 1.000000\n"<<"Tr 1.000000\n"<<"illum 2\n"<<"Ns 0.000000\n"<<"map_Kd ./"<<name<<".png\n";obj.close();}void getobj(Mat gray, string name){assert(gray.type() == CV_8UC1);ofstream obj("output/"+name+".obj");//第一步,写点//从上到下,从左到右//范围是-1到1//mtlobj<<"\nmtllib ./"<<name<<".mtl"<<"\n\n";getmtl(name);int rows = gray.rows;int cols = gray.cols;int z = 255*5;cout<<"points\n";for (int i = 0; i< rows; i+=10){for (int j = 0; j< cols; j+=10){double x = j*1.0/cols;double y = 1-i*1.0/rows;double zz = gray.at<uchar>(i, j)*1.0/z;obj<<"v "<<x<<" "<<y<<" "<<zz<<"\n";}}//第二步, 写材质obj<<"\nusemtl material_0"<<"\n\n";cout<<"textures\n";for (int i = 0; i< rows; i+=10){for (int j = 0; j< cols; j+=10){double s = j*1.0/cols;double t = 1-i*1.0/rows;obj<<"vt "<<s<<" "<<t<<"\n";}}//第三步, 写面obj<<"\n";cout<<"faces\n";rows = rows/10;cols = cols/10;for (int i = 0; i< rows-1; i++){for (int j = 0; j< cols-1; j++){//以(i, j)为顶点,会形成两个三角形//第一个是:(i, j), (i, j+1), (i+1, j+1),索引是:i*cols+j+1//第二个是:(i, j), (i+1, j+1), (i+1, j)/*obj<<"f "<<(i*cols+j+1)<<"/"<<((i*cols+j+1))<<" "<<(i*cols+j+1+1)<<"/"<<((i*cols+j+1+1))<<" "<<((i+1)*cols+j+1+1)<<"/"<<((i+1)*cols+j+1+1)<<"\n";obj<<"f "<<(i*cols+j+1)<<"/"<<((i*cols+j+1))<<" "<<((i+1)*cols+j+1+1)<<"/"<<((i+1)*cols+j+1+1)<<" "<<((i+1)*cols+j+1)<<"/"<<((i+1)*cols+j+1)<<"\n";*/obj<<"f "<<(i*cols+j+1)<<"/"<<((i*cols+j+1))<<" "<<((i+1)*cols+j+1+1)<<"/"<<((i+1)*cols+j+1+1)<<" "<<(i*cols+j+1+1)<<"/"<<((i*cols+j+1+1))<<"\n";obj<<"f "<<(i*cols+j+1)<<"/"<<((i*cols+j+1))<<" "<<((i+1)*cols+j+1)<<"/"<<((i+1)*cols+j+1)<<" "<<((i+1)*cols+j+1+1)<<"/"<<((i+1)*cols+j+1+1)<<"\n";}}obj<<"\n";obj.close();}int main(){string name[] ={"candies", "draws", "girls", "seal", "sheep", "taxi"};for (int seg = 0; seg<6; seg++){Mat test = imread("output/"+name[seg]+"_gray.png", 0);getobj(test, name[seg]);}return 0;}
over。enjoy it。
0 0
- 将jet图像改为灰度图
- OpenCV将彩色图像转化为灰度图代码
- 用matlab将灰度图像矩阵画成三维图
- opencv将灰度图转化为RGB三通道图像
- OpenCV-将图像转化为灰度图,然后边缘检测
- 图像转灰度图
- MATLAB 图像处理 灰度图 将图像中某一行或者某一列的灰度值 绘制成一条曲线
- 将视频图像转化为灰度视频
- 使用 Java 进行图像处理 - 将彩色图像转换为灰度图
- 将各种格式的图像转换为灰度图像
- C#将RGB图像转换为8位灰度图像
- C#将RGB图像转换为8位灰度图像
- vc将彩色图像转换为灰度图像
- 六.使用OpenCv将图像转化为灰度图像
- 【Matlab】将彩色图像转换为灰度图像
- 使用CImage类将RGB图像转化为灰度图像
- MATLAB将彩色图像转化为灰度图像
- 将各种格式的图像转换为灰度图像
- nginx 源码学习(六) 基本数据结构 ngx_array_t
- Java 7之多线程 - 线程的取消与关闭
- linux静态链接库与动态链接库
- 图片垂直居中的使用技巧
- 记录
- 将jet图像改为灰度图
- java中的注解
- if you have installed spm, it maybe you haven't set a NODE_PATH environment variable
- SVN实现版本库中目录的移动(含历史记录的)
- 内核对象kobject架构图和系统设备架构图实例
- 使用setuptools在linux上安装python模块
- 两两之积的最小值是几
- Android上HDMI介绍(基于高通平台)(转)
- hdu 1907 John,hdu 2509 Be the Winner anti-NIM