#include "imageprocessing.h"
#include<QDebug>
#include<math.h>
ImageProcessing::ImageProcessing()
{
currentImageData = NULL;
}
void ImageProcessing::getImageInfo(const QImage targetImage)
{
depth = targetImage.depth();
width = targetImage.width();
height = targetImage.height();
bytesOfPerLine = targetImage.bytesPerLine();
bytesCount = targetImage.byteCount();
format = targetImage.format();
}
void ImageProcessing::countOfEachPixel(BYTE *pImageData, LONG *grayCount)
{
//把grayCount元素置零
for(int k=0;k<256;k++)
grayCount[k] = 0;
int i,j;
BYTE *pImage;
for(i = 0;i < height;i++)
{
for(j = 0;j < width;j++)
{
if(8 == depth) //8位值索引
{
pImage = pImageData + i * bytesOfPerLine + j;
grayCount[*pImage]++;
}
// grayValue = 0.299 * R + 0.587 * G + 0.114 * B
else if(32 == depth) //32位值索引
{
pImage = pImageData + i * bytesOfPerLine + 4 * j;
// LONG value = (*(pImage+1)*30 + *(pImage+2)*59 + *(pImage+3)*11) / 100;//RGB转换为灰度
LONG value = *(pImage)*0.114 +*(pImage + 1)*0.587 + *(pImage + 2)*0.299;
grayCount[value]++;
}
else
{
qDebug() << "error." <<endl;
}
}
}
}
//图像的平移pSrcData代表原始数据的指针,pDestData代表处理后的数据的指针,xoffset代表水平偏移量,yoffse代表垂直偏移量
//如果直接改变pDestData,程序运行过程会发生错误,但是,如果通过中间变量temp=pDestData,
//temp=pDestData + i * bytesOfPerLine + j,程序就不会发生内存错误.为什么?
void ImageProcessing::translationImage(BYTE *pSrcData, BYTE *pDestData, int xoffset, int yoffset)
{
int src_i,src_j,dest_i,dest_j;
BYTE *pTempSrc,*pTempDest;
for(src_i = 0 ,dest_i = yoffset;dest_i < height;src_i++,dest_i++)
{
for(src_j = 0,dest_j = xoffset;dest_j < width;src_j++,dest_j++)
{
if(8 == depth)
{
pTempSrc = pSrcData + src_i * bytesOfPerLine + src_j;
pTempDest = pDestData + dest_i * bytesOfPerLine + dest_j;
*pTempDest = *pTempSrc;
}
if(32 == depth)
{
pTempSrc = pSrcData + src_i * bytesOfPerLine + 4*src_j;
pTempDest = pDestData + dest_i * bytesOfPerLine + 4*dest_j;
*pTempDest = *pTempSrc;
*(pTempDest+1) = *(pTempSrc+1);
*(pTempDest+2) = *(pTempSrc+2);
*(pTempDest+3) = *(pTempSrc+3);
}
}
}
//把平移出去的像素点赋值为255
for(dest_i = 0;dest_i < yoffset;dest_i++)
{
for(dest_j = 0;dest_j < width;dest_j++)
{
if(8 == depth)
{
pTempDest = pDestData + dest_i * bytesOfPerLine + dest_j;
*pTempDest = 255;
}
else if(32 == depth)
{
pTempDest = pDestData + dest_i * bytesOfPerLine + 4*dest_j;
*pTempDest = 255;
*(pTempDest+1) = 255;
*(pTempDest+2) = 255;
*(pTempDest+3) = 255;
}
}
}
for(dest_i = yoffset;dest_i < height;dest_i++)
{
for(dest_j = 0;dest_j < xoffset;dest_j++)
{
if(8 == depth)
{
pTempDest = pDestData + dest_i * bytesOfPerLine + dest_j;
*pTempDest = 255;
}
else if(32 == depth)
{
pTempDest = pDestData + dest_i * bytesOfPerLine + 4*dest_j;
*pTempDest = 255;
*(pTempDest+1) = 255;
*(pTempDest+2) = 255;
*(pTempDest+3) = 255;
}
}
}
}
//图像镜像变换,pSrcData代表源数据指针,pDestData代表处理后的数据指针,direct代表方向:垂直或者水平
//direct=true则表示水平镜像变换,false表示垂直镜像变换
//原理:水平变换公式:x1=width-x0,y1=x1(xo,y0)为原始数据坐标,(x1,y1)为变换后的坐标
//垂直变换公式:x0=x1,y0=height-y1
//这段代码有待改善,可以更精简
void ImageProcessing::miirroredTran(BYTE *pSrcData, BYTE *pDestData, bool direct)
{
int src_i,src_j,dest_i,dest_j;
BYTE *pTempSrc,*pTempDest;
if(direct) //如果是水平镜像对称
{
for(src_i = 0,dest_i = 0;dest_i < height;src_i++,dest_i++)
{
for(src_j = 0,dest_j = width-1;dest_j >=0;src_j++,dest_j--)
{
if(8 == depth)
{
pTempSrc = pSrcData + src_i * bytesOfPerLine + src_j;
pTempDest = pDestData + dest_i * bytesOfPerLine + dest_j;
*pTempDest = *pTempSrc;
}
if(32 == depth)
{
pTempSrc = pSrcData + src_i * bytesOfPerLine + 4*src_j;
pTempDest = pDestData + dest_i * bytesOfPerLine + 4*dest_j;
*pTempDest = *pTempSrc;
*(pTempDest+1) = *(pTempSrc+1);
*(pTempDest+2) = *(pTempSrc+2);
*(pTempDest+3) = *(pTempSrc+3);
}
}
}
}
else
{
for(src_i = 0,dest_i = height-1;dest_i >= 0;src_i++,dest_i--)
{
for(src_j = 0,dest_j = 0;dest_j < width;src_j++,dest_j++)
{
if(8 == depth)
{
pTempSrc = pSrcData + src_i * bytesOfPerLine + src_j;
pTempDest = pDestData + dest_i * bytesOfPerLine + dest_j;
*pTempDest = *pTempSrc;
}
if(32 == depth)
{
pTempSrc = pSrcData + src_i * bytesOfPerLine + 4*src_j;
pTempDest = pDestData + dest_i * bytesOfPerLine + 4*dest_j;
*pTempDest = *pTempSrc;
*(pTempDest+1) = *(pTempSrc+1);
*(pTempDest+2) = *(pTempSrc+2);
*(pTempDest+3) = *(pTempSrc+3);
}
}
}
}
}
//图像转置,输入参数pSrcData:准备转置的图像的首地址,pDestData:转置后的图像的首地址
void ImageProcessing::transpositionImage(BYTE *pSrcData, BYTE *pDestData)
{
BYTE *pTempSrc,*pTempDest;
long bytesOfPerColumn; //每列的字节个数
int i,j;
for(i = 0;i < height;i++)
{
for(j = 0;j < width;j++)
{
if(8 == depth)
{
bytesOfPerColumn = height ;
pTempSrc = pSrcData + i * bytesOfPerLine +j;
pTempDest = pDestData + j * bytesOfPerColumn + i;
*pTempDest = *pTempSrc;
}
if(32 == depth)
{
bytesOfPerColumn = height *4;
pTempSrc = pSrcData + i * bytesOfPerLine + 4*j;
pTempDest = pDestData + j * bytesOfPerColumn + 4*i;
*pTempDest = *pTempSrc;
*(pTempDest+1) = *(pTempSrc+1);
*(pTempDest+2) = *(pTempSrc+2);
*(pTempDest+3) = *(pTempSrc+3);
}
}
}
}
//图像梯度锐化,输入参数pSrcData:准备转置的图像的首地址,pDestData:转置后的图像的首地址
//算法原理:g(i,j) = |f(i,j) - f(i+1,j)| + |f(i,j) - f(i,j+1)|
//Robert梯度:g(i,j) = |f(i,j) - f(i+1,j+1)| + |f(i+1,j) - f(i,j+1)|
void ImageProcessing::gradientSharpen(BYTE *pSrcData, BYTE *pDestData)
{
int i,j;
BYTE *pTempSrc,*pTempDest;
for(i = 0;i < height - 1;i++)
{
for(j = 0;j < width - 1;j++)
{
if(8 == depth)
{
pTempSrc = pSrcData + i * bytesOfPerLine +j;
pTempDest = pDestData + i * bytesOfPerLine + j;