Image compression using median filter in opencv but don't use median filter function
I wrote the program for median filter using c language.i assigned unsigned int array values to cvscalar value.but i cant combine the RGB values for output image.i got separate colors in cvScalar.how to combine them? here i attached my program,
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxtypes.h"
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
IplImage *image = 0;
image = cvLoadImage( "D:/ragavan/ragav.jpg", 1 );
IplImage *image1=cvCloneImage(image);
cvNamedWindow( "Show", 1 );
cvShowImage( "Show", image );
unsigned int a[400][300],c[400][300];
unsigned int p[9],t=0;
// int height,width,step,channels;
// uchar *data;
int i,j,m,n,k=0,l;
CvScalar s,w;
CvSize size=cvGetSize(image);
unsigned int x=size.height;
unsigned int y=size.width;
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
s=cvGet2D(image,i,j); /*for getting i,j pixels*/
c[i][j]=s.val[0]+s.val[1]+s.val[2]/3;
// printf ("%d\t",c[i][j]);
}
// printf("\n");
}
/*to write median filter pgm without using function*/
for(m=0;m<x;m++)
{
for(n=0;n<y;n++)
{
k=0;
for(i=m;i<m+3;i++)
{
for(j=n;j<n+3;j++)
{
p[k]=c[i][j]; /*assign 2 dimension values to single dimension */
k=k+1;
}
}
for(l=0;l<9;l++)
{
for(k=0;k<8;k++)
{
if(p[k]>p[k+1])
{ /*to sort the image pixels */
t=p[k];
p[k]=p[k+1];
p[k+1]=t;
}
}
}
c[m+1][n+1]=p[4]; /*get yhe middle value and store every 3x3 matrix*/
}
}
for(m=0;m<x;m++)
{
for(n=0;n<y;n++)
{
w.val[0]=c[m][n]; //for blue
//w.val[1]=c[m][n]; //for green
// w.val[2]=c[m][n]; //for red
cvSet2D(image1,m,n,w);
}
}
/*
height = image->height;
width = image->width;
step = image->widthStep;
channels = image->nChannels;
data = (uchar *)image->imageData;
printf("Processing a %dx%d image with %d channels\n",height开发者_如何学C,width,channels); */
cvNamedWindow( "Show1", 1 );
cvShowImage( "Show1", image1 );
cvReleaseImage(&image);
cvReleaseImage(&image1);
cvWaitKey(0);
cvDestroyWindow("show");
cvDestroyWindow("show1");
return 0;
}
A solution, probably not the easiest one could be to store all your w.val[ 0 ] in an image, your w.val[ 1 ] in another one, and the w.val[ 2 ] in a last one, each of these 3 images corresponding to a channel (B,G,R). Then you can combine the 3 images using cvMerge().
See the doc of merge here, it's pretty easy to use
If you can't do like this, just comment this message i'll try to write a longer code... Julien,
精彩评论