#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv/cv.h"
#include "math.h"
#include <QDebug>
#include <QDir>
#include <QImage>
#include <QString>
#include <QFile>
#include <iostream>
#include <math.h>
const double PI = 3.141592653;
using namespace std;
void grayImageEqualization();
void colorImageEqualization();
void graySmoothsuanzi();
void colorSmoothsuanzi();
void Laplacesuanzi();
void Fuliye(int u, int v);
void SeeDate();
void firtest();
void exam();
IplImage* img = 0;
IplImage* imgtest = 0;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//img = cvLoadImage("f:/zongx.jpg");
//img = cvLoadImage("f:/lena.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//cvNamedWindow("exam1");
//colorImageEqualization();
//grayImageEqualization();
//graySmoothsuanzi();
//colorSmoothsuanzi();
//Laplacesuanzi();
//int u = 60, v = 300;
//Fuliye(u , v);
//SeeDate();
//cvShowImage("exam1",img);
firtest();
//exam();
}
float LaplaceCore(uchar* p,int widthStep)
{
return 1.0 / 16 * (0 -*(p - 3 - widthStep) - *(p - widthStep) - *(p + 3 - widthStep)
- *(p - 3) - *(p + 3) + *p * 8 -*(p - 3 + widthStep) - *(p + widthStep) - *(p + 3 + widthStep));
}
void grayImageEqualization()
{
//IplImage* img = cvLoadImage("f:/121.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cvNamedWindow("exam",0);
cvNamedWindow("exam2",0);
cvShowImage("exam",img);
const int GRAY_VALUE = 256;
uchar* p = (uchar*)img->imageData;
int GrayPoint[GRAY_VALUE] ={0};
int sumb = 0;
//int RedPoint[GRAY_VALUE] = {0};
//int GreenPoint[GRAY_VALUE] = {0};
//int BluePoint[GRAY_VALUE] = {0};
for(int i = 0;i<(img->widthStep) * (img->height);i++)
{
GrayPoint[*p]++;
p++;
//BluePoint[*p++]++;
//GreenPoint[*p++]++;
// RedPoint[*p++]++;
}
for(int j = 0;j<GRAY_VALUE;j++)
{
//qDebug() <<j<< BluePoint[j] << GreenPoint[j] << RedPoint[j];
//qDebug() << GrayPoint[j];
sumb += GrayPoint[j];
}
//qDebug() << "sumb" << sumb;
int s[GRAY_VALUE] = {0};
int k = 0;
double sum[GRAY_VALUE] ={0};
for(k = 0; k < GRAY_VALUE; k ++)
{
for(int y = 0;y <= k;y++)
{
sum[k] = sum[k] + GrayPoint[y];
}
sum[k] = sum[k]/sumb;
sum[k] = sum[k]*255;
s[k] = cvRound(sum[k]);
//qDebug() << sum[k];
}
p = (uchar*)img->imageData;
for(int i = 0;i<(img->widthStep) * (img->height);i++,p++)
{
for(k = 0; k < GRAY_VALUE; k ++)
{
//qDebug() << *p << k;
//cvWaitKey(10);
if(*p == k)
{
*p = s[k];
//qDebug() << s[k];
break;
}
}
}
cvShowImage("exam2",img);
}
void colorImageEqualization()
{
//IplImage* img = cvLoadImage("f:/2.jpg");
cvNamedWindow("exam4",0);
cvShowImage("exam4",img);
cvNamedWindow("exam3",0);
const int GRAY_VALUE = 256;
uchar* p = (uchar*)img->imageData;
int sumb = 0;
int sumg = 0;
int sumr = 0;
int RedPoint[GRAY_VALUE] = {0};
int GreenPoint[GRAY_VALUE] = {0};
int BluePoint[GRAY_VALUE] = {0};
for(int i = 0;i<(img->width) * (img->height);i++)
{
BluePoint[*p++]++;
GreenPoint[*p++]++;
RedPoint[*p++]++;
}
for(int j = 0;j<GRAY_VALUE;j++)
{
sumb += BluePoint[j];
sumg += GreenPoint[j];
sumr += RedPoint[j];
}
int sb[GRAY_VALUE] = {0};
int sg[GRAY_VALUE] = {0};
int sr[GRAY_VALUE] = {0};
int k = 0;
double sumbb[GRAY_VALUE] ={0};
double sumgg[GRAY_VALUE] ={0};
double sumrr[GRAY_VALUE] ={0};
for(k = 0; k < GRAY_VALUE; k ++)
{
for(int y = 0;y <= k;y++)
{
sumbb[k] = sumbb[k] + BluePoint[y];
sumgg[k] = sumgg[k] + GreenPoint[y];
sumrr[k] = sumrr[k] + RedPoint[y];
}
sumbb[k] = sumbb[k]/sumb;
sumgg[k] = sumgg[k]/sumg;
sumrr[k] = sumrr[k]/sumb;
sumbb[k] = sumbb[k]*255;
sumgg[k] = sumgg[k]*255;
sumrr[k] = sumrr[k]*255;
sb[k] = cvRound(sumbb[k]);
sg[k] = cvRound(sumgg[k]);
sr[k] = cvRound(sumrr[k]);
//qDebug() << sb[k] << sg[k] << sr[k];
}
p = (uchar*)img->imageData;
for(int i = 0;i<(img->widthStep) * (img->height);i++)
{
for(k = 0; k < GRAY_VALUE; k ++)
{
//qDebug() << *p << k;
//cvWaitKey(10);
if(*p == k)
{
*p = sb[k];
p++;
//qDebug() << s[k];
break;
}
if(*p == k)
{
*p = sg[k];
p++;
//qDebug() << s[k];
break;
}
if(*p == k)
{
*p = sr[k];
p++;
//qDebug() << s[k];
break;
}
}
}
cvShowImage("exam1",img);
}
void graySmoothsuanzi()
{
//IplImage* img = cvLoadImage("f:/test.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//cvNamedWindow("exam6");
//cvShowImage("exam6",imgtest);
//cvNamedWindow("exam5");
uchar* m = (uchar*)imgtest->imageData;
for(int i = 1;i < imgtest->height - 1;i++)
for(int k = 1;k < imgtest->widthStep - 1;k++)
{
uchar *p = m + i * imgtest->widthStep + k;
float v = 1.0 / 16 * (*(p - 1 - img->widthStep) + *(p - img->widthStep) * 2 + *(p + 1 - img->widthStep)
+ *(p - 1) * 2 + *(p + 1) * 2 + *p * 4 + *(p - 1 + img->widthStep) + *(p + img->widthStep) * 2 + *(p + 1 +img->widthStep));
*p = (uchar)v;
}
cvShowImage("exam5",imgtest);
}
void colorSmoothsuanzi()
{
//IplImage* img = cvLoadImage("f:/lena.jpg");
cvNamedWindow("exam6");
cvShowImage("exam6",imgtest);
cvNamedWindow("exam5");
uchar* m = (uchar*)img->imageData;
for(int i = 1;i < img->height - 1;i++)
for(int k = 1;k < img->width - 1;k++)
{
uchar *p = m + i * img->widthStep + k*3;
float v = 1.0 / 16 * (*(p - 3 - img->widthStep) + *(p - img->widthStep) * 2 + *(p + 3 - img->widthStep)
+ *(p - 3) * 2 + *(p + 3) * 2 + *p * 4 + *(p - 3 + img->widthStep) + *(p + img->widthStep) * 2 + *(p + 3 +img->widthStep));
*p = (uchar)v;
//qDebug() << v;
p++;
v = 1.0 / 16 * (*(p - 3 - img->widthStep) + *(p - img->widthStep) * 2 + *(p + 3 - img->widthStep)
+ *(p - 3) * 2 + *(p + 3) * 2 + *p * 4 + *(p - 3 + img->widthStep) + *(p + img->widthStep) * 2 + *(p + 3 +img->widthStep));
*p = (uchar)v;
//qDebug() << v;
p++;
v = 1.0 / 16 * (*(p - 3 - img->widthStep) + *(p - img->widthStep) * 2 + *(p + 3 - img->widthStep)
+ *(p - 3) * 2 + *(p + 3) * 2 + *p * 4 + *(p - 3 + img->widthStep) + *(p + img->widthStep) * 2 + *(p + 3 +img->widthStep));
*p = (uchar)v;
//qDebug() << v;
}
cvShowImage("exam2",img);
// IplImage* r = cvCreateImage(cvSize(img->width,img->height),img->depth,1);
// IplImage* g = cvCreateImage(cvSize(img->width,img->height),img->depth,1);
// IplImage* b = cvCreateImage(cvSize(img->width,img->height),img->depth,1);
// cvSplit(img,r,g,b,NULL);
// cvNamedWindow("r",0);
// cvNamedWindow("g",0);
// cvNamedWindow("b",0);
// cvShowImage("r",r);
// cvShowImage("g",g);
// cvShowImage("b",b);
// cvWaitKey(0);
}
void Laplacesuanzi()
{
//IplImage* img = cvLoadImage("f:/lena.jpg");
cvNamedWind