实现真彩图到256色图的转换。它的思想是:准备一个长度为4096的数组,代表4096种颜色。对图中的每一个象素,取R、G、B的最高四位,拼成一个12位的整数,对应的数组元素加1。全部统计完后,就得到了这4096种颜色的使用频率。前256种颜色就是用的最多的颜色
#include "cv.h"
#include "highgui.h"
using namespace cv;
#define PI 3.1415926
#define RADIAN(angle) ((angle)*PI/180.0)
struct RGBColor
{
uchar red;
uchar green;
uchar blue;
};
//RGB转YUV 灰度化
void ColorToGray(Mat& mat)
{
int M=mat.rows;
int N=mat.cols;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
Vec3b bgr=mat.at<Vec3b>(i,j);
float Y=(float)(bgr[2]*0.299+bgr[1]*0.587+bgr[0]*0.114);
bgr[0]=bgr[1]=bgr[2]=(uchar)Y;
mat.at<Vec3b>(i,j)=bgr;
}
}
}
//真彩图转256色图
void TrueTo256(Mat& mat)
{
int M=mat.rows;
int N=mat.cols;
int colorHits[4096]={0};
int colorIndex[4096]={0};
int clrIndex=0;
//取RGB高4位组成一个12bit的整数,