C++ C# python下的中值滤波

C++ 中值滤波

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
#include <time.h>

using namespace cv;

Mat Median_filter(Mat img, int kernel_size) {
	int height = img.rows;
	int width = img.cols;
	int channel = img.channels();
	Mat out = Mat::zeros(height, width, CV_8UC3);
	int pad = floor(kernel_size / 2);
	double v = 0;
	int *vs = new int[kernel_size*kernel_size];
	int count = 0;

	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			for (int c = 0; c < channel; c++) {
				v = 0;
				count = 0;
				for (int i = 0; i < kernel_size*kernel_size; i++)
				{
					vs[i] = 999;
				}
				for (int dy = -pad; dy < pad + 1; dy++) {
					for (int dx = -pad; dx < pad + 1; dx++) {
						if (((y + dy) >= 0) && ((x + dx) >= 0) && ((y + dy) < height) && ((x + dx) < width)) {
							vs[count++] = (int)img.at<cv::Vec3b>(y + dy, x + dx)[c];
						}
					}
				}
				std::sort(vs, vs + (kernel_size*kernel_size));
				out.at<cv::Vec3b>(y, x)[c] = (uchar)vs[int(floor(count / 2)) + 1];
			}
		}
	}
	delete[] vs;
	return out;
}



int main()
{
	clock_t start, finish;
    std::cout << "Hello World!\n";
	Mat img = imread("timg.jpg", IMREAD_COLOR);

	start = clock();
	Mat out = Median_filter(img, 7);
	finish = clock();

	std::cout << (finish - start)*1.0 / CLOCKS_PER_SEC << std::endl;

	imshow("out", out);
	waitKey(0);	
	destroyAllWindows();
}

 

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Diagnostics;

namespace testMedianFilterCS
{
    class Program
    {
        static Mat Median_filter(Mat img, int kernel_size)
        {
            int height = img.Rows;
            int width = img.Cols;
            int channel = img.Dims;
            Mat imgOut = Mat.Zeros(height, width, Emgu.CV.CvEnum.DepthType.Cv8U, channel);
            int pad = kernel_size / 2;
            double v = 0;            
            int[] vs = new int[kernel_size * kernel_size];
            int count = 0;
            Image<Bgr, byte> srcImage = img.ToImage<Bgr, byte>();
            Image<Bgr, byte> outImg = img.ToImage<Bgr, byte>();           
            for (int y=0;y<height;y++)
            {
                for(int x=0;x<width;x++)
                {
                    for(int c=0;c<channel;c++)
                    {
                        v = 0;
                        count = 0;
                        for(int dy=-pad;dy<pad+1;dy++)
                        {
                            for(int dx=-pad;dx<pad+1;dx++)
                            {
                                if(((y+dy)>=0)&&((x+dx)>=0)&&((y+dy)<height)&&((x+dx)<width))
                                {
                                    vs[count++] = (byte)(srcImage.Data[y + dy, x + dx, c]);
                                }
                            }                           
                        }
                        Array.Sort(vs);
                        outImg.Data[y, x, c] = (byte)(vs[count / 2 + 1]);
                    }
                }
            }
            CvInvoke.BitwiseAnd(outImg, outImg, imgOut);
            return imgOut;
        }


        static void Main(string[] args)
        {
            Mat img = CvInvoke.Imread("timg.jpg", Emgu.CV.CvEnum.ImreadModes.Color);
           // CvInvoke.Imshow("out", img);

            Stopwatch st = new Stopwatch();
            st.Start();
            Mat outimg = Median_filter(img, 7);
            st.Stop();

            Console.WriteLine(st.Elapsed.TotalMilliseconds.ToString());
            CvInvoke.Imshow("out2", outimg);           
            CvInvoke.WaitKey(0);
        }       

    }
}

python

import cv2
import numpy as np
import time

def median_filter(img,K_size=3):
    H,W,C=img.shape
    pad=K_size//2
    out=np.zeros((H+pad*2,W+pad*2,C),dtype=np.float)
    out[pad:pad+H,pad:pad+W]=img.copy().astype(np.float)

    tmp=out.copy()

    for y in range(H):
        for x in range(W):
            for c in range(C):
                out[pad+y,pad+x,c]=np.median(tmp[y:y+K_size,x:x+K_size,c])

    out=out[pad:pad+H,pad:pad+W].astype(np.uint8)

    return  out

img=cv2.imread("timg.jpg")

t1=time.time()

out=median_filter(img)

t2=time.time()
out2=cv2.medianBlur(img,3)
t3=time.time()

st1=t2-t1
st2=t3-t2
cv2.imshow("img",img)
cv2.imshow("out",out)
cv2.imshow("out2",out2)
print(st1)
print(st2)
cv2.waitKey(0)

先这样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值