图像处理(三)彩图的中值滤波与cuda形式

灰度图像为一通道图像,如8位的灰度图的每一个像素点的数据由一个uchar类型来储存,uchar的值代表图像的亮度,灰度图像的中值滤波是根据像素值的3×3的领域内,像素值排序后的中值像素代表该点的像素值值;
而彩色图像一般为RGB图片,像素点的颜色由红绿蓝三通道决定,数据由一个uchar3型存储,如同灰度图的中值滤波一样,彩色图像的中值滤波也是根据图像的亮度来选取相应的像素点的值。
RGB图像的亮度计算:
F(x,y)=(R(x,y)+G(x,y)+B(x,y))/3
F(X,Y)是处理后该点的亮度值,R(x,y),B(x,y),G(x,y)是该点的RGB值
(这里说明一下,在HSV模型中亮度V选取的值是RGB的最大值);
根据得的亮度值来滤波,这时得倒的图像是彩色图像的灰度图。
通过

cucun[0] = (temp[offset - ncols - 1].x + temp[offset - ncols - 1].y + temp[offset - ncols - 1].z) / 3;

来计算像素的亮度值;

在c中是直接用两个循环遍历图像来选取该位置像素的亮度值从而决定该点的色彩的值;
在CUDA C中直接申明多线程来处理图像
完整代码:


#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include<opencv2\opencv.hpp>
#include<opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>


using namespace cv;
using namespace std;


__global__ void colorkernel(uchar3 *temp, uchar3 *dirtemp, int ncols, int nrows)
{
    int x = threadIdx.x + blockIdx.x*blockDim.x;
    int y = threadIdx.y + blockIdx.y*blockDim.y;
    int offset = x + y*ncols;
    uchar cucun[9];
    uchar3* cucuny[9];
    uchar3*  t;
    uchar zs;
    cucun[0] = (temp[offset - ncols - 1].x + temp[offset - ncols - 1].y + temp[offset - ncols - 1].z) / 3;
    cucun[1] = (temp[offset - ncols].x + temp[offset - ncols].y + temp[offset - ncols].z) / 3;
    cucun[2] = (temp[offset - ncols + 1].x + temp[offset - ncols + 1].y + temp[offset - ncols + 1].z) / 3;
    cucun[3] = (temp[offset - 1].x + temp[offset - 1].y + temp[offset - 1].z) / 3;
    cucun[4] = (temp[offset].x + temp[offset].y + temp[offset].z) / 3;
    cucun[5] = (temp[offset + 1].x + temp[offset + 1].y + temp[offset + 1].z) / 3;
    cucun[6] = (temp[offset + ncols - 1].x + temp[offset + ncols - 1].y + temp[offset + ncols - 1].z) / 3;
    cucun[7] = (temp[offset + ncols].x + temp[offset + ncols].y + temp[offset + ncols].z) / 3;
    cucun[8] = (temp[offset + ncols + 1].x + temp[offset + ncols + 1].y + temp[offset + ncols + 1].z) / 3;
    cucuny[0] = &temp[offset - ncols - 1];
    cucuny[1] = &temp[offset - ncols];
    cucuny[2] = &temp[offset - ncols + 1];
    cucuny[3] = &temp[offset - 1];
    cucuny[4] = &temp[offset];
    cucuny[5] = &temp[offset + 1];
    cucuny[6] = &temp[offset + ncols - 1];
    cucuny[7] = &temp[offset + ncols];
    cucuny[8] = &temp[offset + ncols + 1];
    if (x > 1 && x < ncols&&y>1 && y < nrows)
    {
        for (int i = 8; i >= 0; i--)
        {
            for (int j = 0; j < i; j++)
            {
                if (cucun[j]>cucun[i])
                {
                    zs = cucun[j];
                    cucun[j] = cucun[i];
                    cucun[i] = zs;
                    t = cucuny[j];
                    cucuny[j] = cucuny[i];
                    cucuny[i] = t;

                }
            }
        }



    }
    dirtemp[offset] = *cucuny[8];


}

void colblur(uchar3 *temp, uchar3 *dirtemp, int ncols, int nrows)
{
    dim3 threads(16, 16);
    dim3 blocks((nrows + threads.x - 1) / threads.x, (nrows + threads.y - 1) / threads.y);

    colorkernel << <blocks, threads >> >(temp, dirtemp, ncols, nrows);
}

void main()
{
    uchar3 *temp, *dirtmep;

    Mat src = imread("1.jpg");
    Mat dir(src.rows, src.cols, CV_8UC3);
    int size = src.rows*src.cols*sizeof(uchar3);
    cudaMalloc((void **)&temp, size);
    cudaMalloc((void**)&dirtmep, size);
    cudaMemcpy(temp, src.data, size, cudaMemcpyHostToDevice);
    colblur(temp, temp, src.cols, src.cols);
    cudaMemcpy(dir.data, temp, size, cudaMemcpyDeviceToHost);
    imshow("12.jpg", dir);
    waitKey();
    cudaFree(temp);
    cudaFree(dirtmep);

}

这仅仅是按公式进行实现,效率较低。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 主要工作: 基于MATLAB图像处理中值滤波、均值滤波以及高斯滤波的实现与对比: a) 中值滤波法是一种非线性平滑技术,它将每一像素点的灰度值设置为该点某邻域窗口内的所有像素点灰度值的中值. b) 均值滤波是典型的线性滤波算法,它是指在图像上对目标像素给一个模板,该模板包括了其周围的临近像素(以目标像素为中心的周围8个像素,构成一个滤波模板,即去掉目标像素本身),再用模板中的全体像素的平均值来代替原来像素值。 c) 高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程。通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个像素点的值,都由其本身和邻域内的其他像素值经过加权平均后得到。高斯滤波的具体操作是:用一个模板(或称卷积、掩模)扫描图像中的每一个像素,用模板确定的邻域内像素的加权平均灰度值去替代模板中心像素点的值。 2. 代码功能: 实现中值滤波、均值滤波以及高斯滤波,并对图像进行输出 3. 结果分析 a) 图像经过中值滤波后,高斯噪声没有被完全去除,椒盐噪声几乎被完全去除效果较好。经过均值滤波后不管是高斯噪声还是椒盐噪声大部分都没有被去除,只是稍微模糊化。经过高斯滤波后,高斯噪声和椒盐噪声几乎被很大程度的模糊化,原图好像被加上了一层蒙版。 【注】若添加图片分辨率过高会发出警报,如果可以正常输出则可以忽视。
中值滤波是一种常用的图像处理方法,在MATLAB中也有相应的函数可以实现中值滤波。使用中值滤波可以有效地去除图像中的噪声,同时保持图像的边缘和细节信息。中值滤波的原理很简单,就是通过计算一个像素周围邻域内像素的中值来替代该像素的值。 在MATLAB中,可以使用medfilt2函数来实现中值滤波。该函数的第一个参数是待处理的图像,可以是灰度图像或彩色图像。如果是彩色图像,需要分别对每个通道进行中值滤波。第二个参数是滤波窗口的大小,一般选择一个奇数大小的正方形窗口,例如[3 3]表示3x3的窗口。 下面是一个示例代码,演示了如何在MATLAB中对灰度图像和彩色图像进行中值滤波: ```matlab % 对灰度图像进行中值滤波 image = imread('image.jpg'); filtered_image = medfilt2(image, [3 3]); % 对彩色图像进行中值滤波 color_image = imread('color_image.jpg'); filtered_color_image = color_image; filtered_color_image(:,:,1) = medfilt2(color_image(:,:,1), [3 3]); filtered_color_image(:,:,2) = medfilt2(color_image(:,:,2), [3 3]); filtered_color_image(:,:,3) = medfilt2(color_image(:,:,3), [3 3]); ``` 以上代码分别对灰度图像和彩色图像进行中值滤波,并将结果保存在filtered_image和filtered_color_image变量中。你可以根据自己的需求修改代码中的图像路径和窗口大小。 需要注意的是,中值滤波对于一些特定类型的噪声(如椒盐噪声)效果较好,但对于其他类型的噪声可能效果不佳。在实际应用中,可以根据图像的特点和噪声类型选择合适的滤波方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值