📄 image.cpp
字号:
#include "image.h"
#include <stdio.h>
void remplir(pixeltype &pixel,int type=1)
{
if(type==1)
{
int r,g,b;
QColor inter(pixel.color);
inter.getRgb(&r,&g,&b);
pixel.r=r;
pixel.g=g;
pixel.b=b;
}
else if(type==2)
{
pixel.color=QColor(pixel.r,pixel.g,pixel.b).rgb();
}
}
int valid(int color)
{
if(color>255)color=255;
else if(color<0)color=0;
return color;
}
//************les filters*/
/////////////////////gaussien (moyenne)
QRgb getgaussien(QRgb *tab)
{
int r,g,b;
int resultr=0,resultg=0,resultb=0;
for(int i=0;i<9;i++)
{
QColor *inter=new QColor(tab[i]);
inter->getRgb(&r,&g,&b);
resultr+=r;
resultg+=g;
resultb+=b;
}
resultr/=9;
resultb/=9;
resultg/=9;
return QColor(valid(resultr),valid(resultg),valid(resultb)).rgb();
}
//avec modification
void RImage::gaussien(QImage *image)
{
QRgb tab[9];
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
tab[0]=image->pixel(w-1,h);
tab[1]=image->pixel(w+1,h);
tab[2]=image->pixel(w,h-1);
tab[3]=image->pixel(w,h+1);
tab[4]=image->pixel(w-1,h-1);
tab[5]=image->pixel(w+1,h-1);
tab[6]=image->pixel(w-1,h+1);
tab[7]=image->pixel(w+1,h+1);
tab[8]=image->pixel(w,h);
image->setPixel(w,h,getgaussien(tab));
}
}
//sans modification
void RImage::gaussiensans(QImage *image)
{
pixeltype *gaussien=new pixeltype[image->width()*image->height()];
QRgb tab[9];
int f=0;
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
tab[0]=image->pixel(w-1,h);
tab[1]=image->pixel(w+1,h);
tab[2]=image->pixel(w,h-1);
tab[3]=image->pixel(w,h+1);
tab[4]=image->pixel(w-1,h-1);
tab[5]=image->pixel(w+1,h-1);
tab[6]=image->pixel(w-1,h+1);
tab[7]=image->pixel(w+1,h+1);
tab[8]=image->pixel(w,h);
gaussien[f].color=getgaussien(tab);
gaussien[f].x=w;
gaussien[f].y=h;
f++;
}
for(int i=0;i<f;i++)
image->setPixel(gaussien[i].x,gaussien[i].y,gaussien[i].color);
}
//////////////////////median
QRgb getmedian(QRgb *tab)
{
for(int i=0;i<9;i++)
for(int j=i+1;j<9;j++)
if(tab[i]>tab[j])
{
QRgb inter=tab[i];
tab[i]=tab[j];
tab[j]=inter;
}
return tab[4];
}
//avec modification
void RImage::median(QImage *image)
{
QRgb tab[9];
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
tab[0]=image->pixel(w-1,h);
tab[1]=image->pixel(w+1,h);
tab[2]=image->pixel(w,h-1);
tab[3]=image->pixel(w,h+1);
tab[4]=image->pixel(w-1,h-1);
tab[5]=image->pixel(w+1,h-1);
tab[6]=image->pixel(w-1,h+1);
tab[7]=image->pixel(w+1,h+1);
tab[8]=image->pixel(w,h);
image->setPixel(w,h,getmedian(tab));
}
}
//sans modification
void RImage::mediansans(QImage *image)
{
pixeltype *median=new pixeltype[image->width()*image->height()];
QRgb tab[9];
int f=0;
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
tab[0]=image->pixel(w-1,h);
tab[1]=image->pixel(w+1,h);
tab[2]=image->pixel(w,h-1);
tab[3]=image->pixel(w,h+1);
tab[4]=image->pixel(w-1,h-1);
tab[5]=image->pixel(w+1,h-1);
tab[6]=image->pixel(w-1,h+1);
tab[7]=image->pixel(w+1,h+1);
tab[8]=image->pixel(w,h);
median[f].color=getmedian(tab);
median[f].x=w;
median[f].y=h;
f++;
}
for(int i=0;i<f;i++)
image->setPixel(median[i].x,median[i].y,median[i].color);
}
/////////////////passe haut
QRgb getpassehaut(pixeltype *tab)
{
int resultr=0,resultg=0,resultb=0;
for(int i=0;i<9;i++) remplir(tab[i],1);
for(int i=0;i<8;i++)
{
resultr+=tab[i].r;
resultg+=tab[i].g;
resultb+=tab[i].b;
}
resultr=9*tab[8].r-resultr;
resultg=9*tab[8].g-resultg;
resultb=9*tab[8].b-resultb;
return QColor(valid(resultr),valid(resultg),valid(resultb)).rgb();
}
void RImage::passehaut(QImage *image)
{
pixeltype *passehaut=new pixeltype[image->width()*image->height()];
int f=0;
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
pixeltype tab[9];
tab[0].color=image->pixel(w-1,h);
tab[1].color=image->pixel(w+1,h);
tab[8].color=image->pixel(w ,h);
tab[3].color=image->pixel(w ,h-1);
tab[4].color=image->pixel(w ,h+1);
tab[5].color=image->pixel(w-1,h+1);
tab[6].color=image->pixel(w+1,h+1);
tab[7].color=image->pixel(w+1,h-1);
tab[2].color=image->pixel(w-1,h-1);
passehaut[f].color=getpassehaut(tab);
passehaut[f].x=w;
passehaut[f].y=h;
f++;
}
for(int i=0;i<f;i++)
image->setPixel(passehaut[i].x,passehaut[i].y,passehaut[i].color);
}
//*************les gardients*/
/////////////////////soble
QRgb getsoble(pixeltype *tab)
{
int resultxr=0,resultxg=0,resultxb=0,resultyr=0,resultyg=0,resultyb=0;
int resultr,resultg,resultb;
resultxr=tab[8].r+2*tab[0].r+tab[5].r-tab[7].r-2*tab[1].r-tab[6].r;
resultyr=tab[8].r+2*tab[3].r+tab[7].r-tab[5].r-2*tab[4].r-tab[6].r;
resultxg=tab[8].g+2*tab[0].g+tab[5].g-tab[7].g-2*tab[1].g-tab[6].g;
resultyg=tab[8].g+2*tab[3].g+tab[7].g-tab[5].g-2*tab[4].g-tab[6].g;
resultxb=tab[8].b+2*tab[0].b+tab[5].b-tab[7].b-2*tab[1].b-tab[6].b;
resultyb=tab[8].b+2*tab[3].b+tab[7].b-tab[5].b-2*tab[4].b-tab[6].b;
resultr=(int)sqrt((float)(resultxr*resultxr+resultyr+resultyr));
resultg=(int)sqrt((float)(resultxg*resultxg+resultyg+resultyg));
resultb=(int)sqrt((float)(resultxb*resultxb+resultyb+resultyb));
return QColor(valid(resultr),valid(resultg),valid(resultb)).rgb();
}
pixeltype RImage::soblepixel(QImage *image,int x,int y)
{
pixeltype soble;
pixeltype tab[9];
tab[0].color=image->pixel(x-1,y);
tab[1].color=image->pixel(x+1,y);
tab[2].color=image->pixel(x ,y);
tab[3].color=image->pixel(x ,y-1);
tab[4].color=image->pixel(x ,y+1);
tab[5].color=image->pixel(x-1,y+1);
tab[6].color=image->pixel(x+1,y+1);
tab[7].color=image->pixel(x+1,y-1);
tab[8].color=image->pixel(x-1,y-1);
for(int i=0;i<9;i++) remplir(tab[i],1);
soble.x=x;
soble.y=y;
soble.color=getsoble(tab);
return soble;
}
void RImage::soble(QImage *image)
{
pixeltype *soble=new pixeltype[image->width()*image->height()];
int f=0;
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
soble[f]=soblepixel(image,w,h);
f++;
}
for(int i=0;i<f;i++)
image->setPixel(soble[i].x,soble[i].y,soble[i].color);
}
/////////////////simple gradien
QRgb getgradien(pixeltype *tab)
{
int resultxr=0,resultxg=0,resultxb=0,resultyr=0,resultyg=0,resultyb=0;
int resultr,resultg,resultb;
resultxr=tab[2].r-tab[3].r;
resultyr=tab[0].r+tab[1].r;
resultxg=tab[2].g-tab[3].g;
resultyg=tab[0].g+tab[1].g;
resultxb=tab[2].b-tab[3].b;
resultyb=tab[0].b+tab[1].b;
resultr=(int)sqrt((float)(resultxr*resultxr+resultyr+resultyr));
resultg=(int)sqrt((float)(resultxg*resultxg+resultyg+resultyg));
resultb=(int)sqrt((float)(resultxb*resultxb+resultyb+resultyb));
return QColor(valid(resultr),valid(resultg),valid(resultb)).rgb();
}
pixeltype RImage::gradienpixel(QImage *image,int x,int y)
{
pixeltype gradien;
pixeltype tab[4];
tab[0].color=image->pixel(x-1,y);
tab[1].color=image->pixel(x+1,y);
tab[2].color=image->pixel(x ,y-1);
tab[3].color=image->pixel(x ,y+1);
for(int i=0;i<4;i++) remplir(tab[i],1);
gradien.x=x;
gradien.y=y;
gradien.color=getgradien(tab);
return gradien;
}
void RImage::gradien(QImage *image)
{
pixeltype *gradien=new pixeltype[image->width()*image->height()];
int f=0;
for(int h=1;h<image->height()-1;h++)
for(int w=1;w<image->width()-1;w++)
{
gradien[f]=gradienpixel(image,w,h);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -