最小值滤波 (C 语言实现)

2024-06-06 09:58
文章标签 语言 实现 最小值 滤波

本文主要是介绍最小值滤波 (C 语言实现),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

最小值滤波 (C 语言实现)


遇到最小值滤波的问题,小白不知道,一个程序写了三天,终于今天傍晚出来了。。。


很简单的for循环,但是没有理解最小值滤波,怎么写都是错啊~


这是我见过做好的描述,关于最小值滤波:




3*3的像素点阵,对于中心点做最小值滤波的话,它的值将从77变换到0




处理结果图:



我一直支持也坚持开源分享的原则,为大家更好的相互学习,给出源代码

/******************************************************************
code writer : EOF
code date : 2014.08.07
e-mail  : jasonleaster@gmail.com jasonleaster@163.comcode purpose:This demo is coded for mininum value filter.
If you find something wrong with my code, please touch me by e-mail.
Thank you.*******************************************************************/#include "opencv2/highgui/highgui_c.h"
#include "opencv2/imgproc/imgproc_c.h"#include <stdio.h>/*------------------------------------------------------------------------------This two Macro are used for debugging, if you are begginer with OpenCV,
it will help you to know and test what inside of the data struture in OpenCV-------------------------------------------------------------------------------*///#define RGB_TEST_DEBUG 		
//#define CHANNEL_TEST_DEBUG	/* the offset of three channel RGB */
#define RED_BIT   2
#define GREEN_BIT 1
#define BLUE_BIT  0#define SQUARE_LENGTH 15int get_dark_imagine(IplImage* const img_origin,IplImage* const img_win_dark);int main(int argc,char* argv[])
{char* win_name_bf = "Before Processing";char* win_name_af = "After  Processing";CvSize size;IplImage* img_origin = cvLoadImage(argv[1],CV_LOAD_IMAGE_COLOR);size.height = img_origin->height;size.width  = img_origin->width;IplImage* img_win_dark = cvCreateImage(size,IPL_DEPTH_8U,1);//single channelget_dark_imagine(img_origin,img_win_dark);cvNamedWindow(win_name_bf,CV_WINDOW_AUTOSIZE);//cvNamedWindow is a function which would help you to creat a window.cvShowImage(win_name_bf,img_origin);//Obviously, show the picture that you inputed.cvNamedWindow(win_name_af,CV_WINDOW_AUTOSIZE);//cvNamedWindow is a function which would help you to creat a window.cvShowImage(win_name_af,img_win_dark);//Obviously, show the picture that you inputed.cvWaitKey(0);//pause and let the user see the picture.cvReleaseImage(&img_origin);cvReleaseImage(&img_win_dark);//Finally, release the struture, otherwise, memory leak !return 0;
}int get_dark_imagine(IplImage* const img_origin,IplImage* const img_win_dark)
{/*Varible description:@img_origin  : A pointer which point to the original picture's IplImage-structure.@img_win_dark: A pointer which point to the dark-window's IplImage-structure.*/if(img_origin == NULL || img_win_dark == NULL){printf("Error! img_origin or img_win_dark is NULL\n");return 1;}int height_origin = img_origin->height ;int width_origin  = img_origin->width ;//the search band width.unsigned char *  const ptr_array_origin    = (unsigned char*)img_origin->imageData;unsigned char *  const ptr_array_win_dark  = (unsigned char*)img_win_dark->imageData;unsigned char* ptr_header_origin    = NULL;int row = 0;int col = 0;int square_row = 0;int square_col = 0;int min    = 0;int T_min  = 0;int temp_R = 0;int temp_G = 0;int temp_B = 0;int temp   = 0;int search_win_start = SQUARE_LENGTH/2;/*define two varible -- height_origin & width_origin for up band-width of the search-window*/int search_win_height_end  = img_win_dark->height - SQUARE_LENGTH/2;int search_win_width_end   = img_win_dark->width   - SQUARE_LENGTH/2;//initializition of the picture's data that 'ptr_array_win_dark' point to.for(row = 0; row < height_origin; row++){for(col = 0; col < width_origin ;col++){*(ptr_array_win_dark + col + row*(img_win_dark->widthStep)) = 255;}}//Mininum value filter for(row = search_win_start; row < search_win_height_end; row++){for(col = search_win_start; col < search_win_width_end ;col++){ptr_header_origin = ptr_array_origin + (row)*(img_origin->widthStep) + (col)*3;temp_B = *(ptr_header_origin + BLUE_BIT  );temp_G = *(ptr_header_origin + GREEN_BIT );temp_R = *(ptr_header_origin + RED_BIT   );min = (temp_G < temp_B) ? temp_G :  temp_B;min = (min    < temp_R) ? min    :  temp_R;T_min = min;for(square_row = (row - search_win_start); square_row < (row + search_win_start + 1);square_row++){for(square_col = (col - search_win_start); square_col < (col+search_win_start + 1);square_col++){min = *(ptr_array_win_dark + square_col + square_row*(img_win_dark->widthStep));if (min > T_min){*(ptr_array_win_dark + square_col + square_row*(img_win_dark->widthStep)) = (T_min);}}}}}return 0;
}



如有错误,欢迎交流指正

—— EOF


update : 2014.10.05

写了一个matlab版本的最小滤波算法框架

Img_filted = dark_channel;
for row = 1 :  heightfor col = 1 : width            min_value =  dark_channel(row,col);for patch_row = (row  -floor(search_win_height/2)) : (row + floor(search_win_height/2))for patch_col = (col - floor(search_win_width/2)) : (col   + floor(search_win_width/2))if patch_row > 0  &&  patch_col > 0 && patch_row <= height && patch_col <= widthif  min_value < Img_filted(patch_row,patch_col)Img_filted(patch_row,patch_col) = min_value;endendendendend
end







这篇关于最小值滤波 (C 语言实现)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1035784

相关文章

C++中unordered_set哈希集合的实现

《C++中unordered_set哈希集合的实现》std::unordered_set是C++标准库中的无序关联容器,基于哈希表实现,具有元素唯一性和无序性特点,本文就来详细的介绍一下unorder... 目录一、概述二、头文件与命名空间三、常用方法与示例1. 构造与析构2. 迭代器与遍历3. 容量相关4

C++中悬垂引用(Dangling Reference) 的实现

《C++中悬垂引用(DanglingReference)的实现》C++中的悬垂引用指引用绑定的对象被销毁后引用仍存在的情况,会导致访问无效内存,下面就来详细的介绍一下产生的原因以及如何避免,感兴趣... 目录悬垂引用的产生原因1. 引用绑定到局部变量,变量超出作用域后销毁2. 引用绑定到动态分配的对象,对象

SpringBoot基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Python实现字典转字符串的五种方法

《Python实现字典转字符串的五种方法》本文介绍了在Python中如何将字典数据结构转换为字符串格式的多种方法,首先可以通过内置的str()函数进行简单转换;其次利用ison.dumps()函数能够... 目录1、使用json模块的dumps方法:2、使用str方法:3、使用循环和字符串拼接:4、使用字符

Linux下利用select实现串口数据读取过程

《Linux下利用select实现串口数据读取过程》文章介绍Linux中使用select、poll或epoll实现串口数据读取,通过I/O多路复用机制在数据到达时触发读取,避免持续轮询,示例代码展示设... 目录示例代码(使用select实现)代码解释总结在 linux 系统里,我们可以借助 select、

Linux挂载linux/Windows共享目录实现方式

《Linux挂载linux/Windows共享目录实现方式》:本文主要介绍Linux挂载linux/Windows共享目录实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录文件共享协议linux环境作为服务端(NFS)在服务器端安装 NFS创建要共享的目录修改 NFS 配

通过React实现页面的无限滚动效果

《通过React实现页面的无限滚动效果》今天我们来聊聊无限滚动这个现代Web开发中不可或缺的技术,无论你是刷微博、逛知乎还是看脚本,无限滚动都已经渗透到我们日常的浏览体验中,那么,如何优雅地实现它呢?... 目录1. 早期的解决方案2. 交叉观察者:IntersectionObserver2.1 Inter