#include "qmath.h"
#include <opencv2\opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
//如果你不是基于opencv那么将代码中的Mat类全部替换成你自己的图像类即可,其他的不要改
//画出目标图像对于灰度的3维图像(45°)
//img_src原始图,彩图或灰度或黑白
//colortype颜色类型 1:原始图像对应点颜色,2:该点对应的灰度,3:温度颜色
//返回灰度的3维图像
//分别在x,y,z轴倾斜45°角后画出每个像素点的3维值
Mat testopencv::Img_gray_3D(Mat img_src,int colortype)
{
//温度色标卡
float Color[73][3] = { { 2,0,13 },{ 4,0,23 },{ 6,4,15 },{ 6,3,84 },{ 9,6,119 },{ 0,1,136 },{ 0,5,157 },{ 1,17,175 },{ 1,22,201 },{ 3,37,220 },{ 6,59,237 },{ 2,77,244 },{ 5,104,249 },{ 8,128,248 },
{ 9,156,252 },{ 4,175,245 },{ 1,191,243 },{ 7,214,246 },{ 5,229,241 },{ 5,236,242 },{ 11,246,250 },{ 6,245,248 },{ 6,246,247 },{ 6,247,240 },{ 6,249,231 },{ 6,251,218 },{ 6,251,207 },{ 5,253,191 },
{ 5,253,177 },{ 6,250,163 },{ 9,252,146 },{ 7,250,124 },{ 10,250,115 },{ 14,248,101 },{ 18,248,87 },{ 24,249,69 },{ 30,249,57 },{ 45,251,46 },{ 57,252,36 },{ 74,248,37 },{ 77,249,25 },{ 93,249,15 },
{ 109,251,9 },{ 125,250,10 },{ 139,248,15 },{ 152,252,16 },{ 166,251,8 },{ 182,250,7 },{ 193,250,11 },{ 211,254,16 },{ 221,252,11 },{ 234,247,10 },{ 240,242,9 },{ 246,242,13 },{ 251,239,13 },
{ 252,234,10 },{ 250,228,7 },{ 249,213,7 },{ 251,197,11 },{ 248,179,13 },{ 248,160,10 },{ 244,135,8 },{ 245,109,7 },{ 242,80,5 },{ 249,71,11 },{ 247,56,11 },{ 246,54,17 },{ 239,62,28 },{ 250,100,67 },
{ 244,127,94 },{ 243,151,128 },{ 247,188,172 },{ 250,224,211 } };
if (img_src.empty())
return img_src;
Mat img_gray;
if (img_src.channels() == 3) //彩图
cvtColor(img_src, img_gray, CV_BGR2GRAY);
else //灰图
img_src.copyTo(img_gray);
Mat d(img_gray.rows*1.0 / 2.828 + 301 + 50, (img_gray.cols + img_gray.rows*1.0 / 1.414) / 2 + 100, CV_8UC3); //45°视角图
d = Scalar::all(255);
//画坐标轴
line(d, Point(img_gray.rows*1.0 / 2.828 + 50, 300), Point(d.cols - 1, 300), Scalar(0, 0, 0));
line(d, Point(img_gray.rows*1.0 / 2.828 + 50, 0), Point(img_gray.rows*1.0 / 2.828 + 50, 300), Scalar(0, 0, 0));
line(d, Point(0, d.rows), Point(img_gray.rows*1.0 / 2.828 + 50, 300), Scalar(0, 0, 0));
line(d, Point(d.cols - 1, 300), Point(d.cols - 10, 310), Scalar(0, 0, 0));
line(d, Point(d.cols - 1, 300), Point(d.cols - 10, 290), Scalar(0, 0, 0));
line(d, Point(img_gray.rows*1.0 / 2.828 + 50, 0), Point(img_gray.rows*1.0 / 2.828 + 60, 10), Scalar(0, 0, 0));
line(d, Point(img_gray.rows*1.0 / 2.828 + 50, 0), Point(img_gray.rows*1.0 / 2.828 + 40, 10), Scalar(0, 0, 0));
line(d, Point(0, d.rows), Point(8, d.rows - 18), Scalar(0, 0, 0));
line(d, Point(0, d.rows), Point(18, d.rows - 8), Scalar(0, 0, 0));
putText(d, ("gray"), Point(img_gray.rows*1.0 / 2.828 + 10, 20), CV_FONT_HERSHEY_DUPLEX, 0.4f, Scalar(0, 0, 0));
putText(d, ("rows"), Point(5, d.rows - 35), CV_FONT_HERSHEY_DUPLEX, 0.4f, Scalar(0, 0, 0));
putText(d, ("cols"), Point(d.cols - 30, 280), CV_FONT_HERSHEY_DUPLEX, 0.4f, Scalar(0, 0, 0));
Point start, end;
Scalar color;
for (int i = 0; i < img_gray.rows; i += 1)
{
uchar* p = img_gray.ptr<uchar>(i);
for (int j = 0; j < img_gray.cols; j += 1)
{
//d.at<Vec3b>(i*1.0 / 2.828 + 300, (j + gray.rows*1.0 / 1.414 - i*1.0 / 1.414) / 2+50) =Vec3b( p[j],p[j],p[j]);
start = Point((j + img_gray.rows*1.0 / 1.414 - i*1.0 / 1.414) / 2 + 50, i*1.0 / 2.828 + 300);
end = Point((j + img_gray.rows*1.0 / 1.414 - i*1.0 / 1.414) / 2 + 50, i*1.0 / 2.828 + 300 - p[j] / 2);
switch (colortype)
{
case 1:
{
if (img_src.channels() == 3)
color = img_src.at<Vec3b>(i, j);
else
color = Scalar(p[j], p[j], p[j]);
break;
}
case 2:
{
color = Scalar(p[j], p[j], p[j]);
break;
}
case 3:
{
color = Scalar(Color[int(p[j] / 3.8)][2], Color[int(p[j] / 3.8)][1], Color[int(p[j] / 3.8)][0]);
break;
}
default:
color = Scalar(Color[int(p[j] / 3.8)][2], Color[int(p[j] / 3.8)][1], Color[int(p[j] / 3.8)][0]);
break;
}
if (end.y > start.y)
end.y = 2 * start.y - end.y;
line(d, start, end, color);
}
}
//左上角画温度色标卡
for (int i = 0; i < 200; i++)
{
if(colortype==2)
line(d, Point(5, 210 - i), Point(15, 210 - i), Scalar(i*1.275, i*1.275, i*1.275));
else
line(d, Point(5, 210 - i), Point(15, 210 - i), Scalar(Color[int(i / 2.74)][2], Color[int(i / 2.74)][1], Color[int(i / 2.74)][0]));
}
putText(d, ("255"), Point(5, 10), CV_FONT_HERSHEY_DUPLEX, 0.3f, Scalar(0, 0, 0));
putText(d, ("0"), Point(5, 220), CV_FONT_HERSHEY_DUPLEX, 0.3f, Scalar(0, 0, 0));
return d;
}