当前位置: 首页 > news >正文

图像处理算法研究的程序框架

目录

1 程序框架简介

2 C#图像读取、显示、保存模块

3 C动态库图像算法模块

4 C#调用C动态库

5 演示Demo

5.1 开发环境

5.2 功能介绍

5.3 下载地址

参考


1 程序框架简介

        一个图像处理算法研究的常用程序逻辑框架,如下图所示

        在该框架中,将图像处理算法产品分为上层模块和底层模块两个部分。

        底层模块使用C/C++实现算法API,提供给上层模块调用;上层模块执行调用API和一些界面功能的实现,最后得到不同平台的软件产品。

        本文使用使用C/C++进行图像算法API开发,具有安全、高效和方便多平台移植的优点,目前大多数图像软件都是基于C/C++来开发的;使用C#来做上层调用与界面设计,是因为C#具有优秀的面向对象能力,界面设计方便快捷,而且对于图像读取、保存、显示已经做了良好的封装,避免了图像编解码的问题,相对使用MFC大大简化了开发逻辑,提升了开发效率。

2 C#图像读取、显示、保存模块

        代码相对简单,直接给出模块代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;namespace ImageProcessDemo
{public partial class FormMain : Form{public FormMain(){InitializeComponent();}#region  变量名字// 图像文件名称private String curFileName = null;// 当前图像变量private Bitmap curBitmap = null;// 原图private Bitmap srcBitmap = null;#endregion#region  图像打开和保存模块// 图像打开public void OpenFile(){OpenFileDialog ofd = new OpenFileDialog();ofd.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +"*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +"位图( *.bmp; *.jpg; *.png;...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" +"矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";ofd.ShowHelp = true;ofd.Title = "打开图像文件";if (ofd.ShowDialog() == DialogResult.OK){curFileName = ofd.FileName;try{curBitmap = (Bitmap)System.Drawing.Image.FromFile(curFileName);srcBitmap = new Bitmap(curBitmap);}catch (Exception exp){ MessageBox.Show(exp.Message); }}}// 图像保存public void SaveFile(){SaveFileDialog sfd = new SaveFileDialog();sfd.Filter = @"Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg|PNG文件(*.png)|*.png|所有合适文件(*.bmp,*.jpg,*.png)|*.bmp;*.jpg;*.png";sfd.FilterIndex = 3;sfd.RestoreDirectory = true;if (sfd.ShowDialog() == DialogResult.OK){ImageFormat format = ImageFormat.Jpeg;switch (Path.GetExtension(sfd.FileName).ToLower()){case ".jpg":format = ImageFormat.Jpeg;break;case ".bmp":format = ImageFormat.Bmp;break;case ".png":format = ImageFormat.Png;break;default:MessageBox.Show("Unsupported image format was specified!");return;}pictureBox1.Image.Save(sfd.FileName, format);}}// 鼠标按键按下显示原图private void pictureBox1_MouseDown(object sender, MouseEventArgs e){if (srcBitmap != null)pictureBox1.Image = srcBitmap;}// 鼠标按键抬起显示效果图private void pictureBox1_MouseUp(object sender, MouseEventArgs e){if (curBitmap != null)pictureBox1.Image = curBitmap;}// open按钮事件private void btn_open_Click(object sender, EventArgs e){OpenFile();if (curBitmap != null){pictureBox1.Image = (Image)curBitmap;}}// save按钮事件private void btn_save_Click(object sender, EventArgs e){if (pictureBox1.Image != null)SaveFile();}#endregion}
}

3 C动态库图像算法模块

        构建C实现获取像素RGB值的API代码。

#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>#include "ImageProcessAPI.h"int getPixel(unsigned char *srcData, int width, int height, int stride, int x, int y, int rgba[4])
{x = x < 0 ? 0 : (x > width - 1 ? width - 1 : x);y = y < 0 ? 0 : (y > height - 1 ? height - 1 : y);int ret = 0;if(srcData == NULL){printf("input image is null!");return -1;}// 图像处理,获取像素RGBAint pos = x * 4 + y * stride;rgba[0] = srcData[pos + 2];rgba[1] = srcData[pos + 1];rgba[2] = srcData[pos + 0];rgba[3] = srcData[pos + 3];return ret;
};

4 C#调用C动态库

        在C#中调用C/C++动态库需要引用 System.Runtime.InteropServices 命名空间,实例代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;namespace ImageProcessDemo
{unsafe class ImageProcessBitmap{// 引用DLL中的方法,此处ImageProcessDll为DLL的名字[DllImport("ImageProcessDll.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.None, ExactSpelling = true)]// 封装C中的API接口,此处应注意C#和C之间的数据类型转换问题,所有参数的数据类型必须一致// getPixel为C中的接口名称private static extern int getPixel(byte* srcData, int width, int height, int stride, int x, int y, int[] rgba);// C#层接口调用// GetImgPixel为C#中封装的接口名称,该接口调用C中的getPixelpublic Bitmap GetImgPixel(Bitmap src, int x, int y, ref int[] rgba){// 生成图像备份Bitmap a = new Bitmap(src);// 获取图像的宽度和高度int w = a.Width;int h = a.Height;// 获取并锁定图像数据区域// 依据图像格式可选择16/24/32位等格式BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);// 传递给C接口getPixel((byte*)srcData.Scan0, w, h, srcData.Stride, x, y, rgba);// 解锁图像数据区域a.UnlockBits(srcData);return a;}}
}

        在C#界面中增加代码实现:用鼠标单击图像,界面上就会显示对应位置像素的RGB值。

        #region 图像处理,用鼠标单击图像,界面上就会显示对应位置像素的RGB值// 图像处理类ImageProcessBitmap imageProcess = new ImageProcessBitmap();// 变量private int[] rgba = new int[4];private void pictureBox1_MouseMove(object sender, MouseEventArgs e){if (pictureBox1.Image != null){curBitmap = imageProcess.GetImgPixel(srcBitmap, e.X, e.Y, ref rgba);label_rgba.Text = "RGBA: (" + rgba[0].ToString() + "," + rgba[1].ToString() + "," + rgba[2].ToString() + "," + rgba[3].ToString() + ")";}}#endregion

5 演示Demo

5.1 开发环境

  • Windows 10 Pro x64

  • Visual Studio 2015

5.2 功能介绍

        演示程序主界面如下图所示,具有图像读取、显示、保存以及随着光标的滑动会动态显示光标所在位置对应图像像素的RGB值等功能。

5.3 下载地址

        开发环境:

  • Windows 10 pro x64

  • Visual Studio 2015

        下载地址: 图像处理算法研究的程序框架

参考

        图像视频滤镜与人像美颜美妆算法详解. 胡耀武、谭娟、李云夕. 电子工业出版社、2020-07

http://www.lryc.cn/news/526462.html

相关文章:

  • GitLab配置免密登录和常用命令
  • Chameleon(变色龙) 跨平台编译C文件,并一次性生成多个平台的可执行文件
  • 第十四讲 JDBC数据库
  • “AI教学实训系统:打造未来教育的超级引擎
  • java读取设置pdf属性信息
  • C语言内存管理详解
  • mysql从全备文件中提取单库或单表进行恢复——筑梦之路
  • HTML-新浪新闻-实现标题-排版
  • 【前沿聚焦】机器学习的未来版图:从自动化到隐私保护的技术突破
  • 二叉树的最大深度(C语言详解版)
  • 基于dlib/face recognition人脸识别推拉流实现
  • 【kong gateway】5分钟快速上手kong gateway
  • webrtc入门系列(五)amazon-kinesis-video-streams-webrtc-sdk-c编译
  • 通过亚马逊云科技Bedrock打造自定义AI智能体Agent(上)
  • 【Nacos】负载均衡
  • 小智 AI 聊天机器人
  • HTML一般标签和自闭合标签介绍
  • 怎么用u盘怎么重装系统_用u盘重装系统详细图文教程【新手教程】
  • 记录一次k8s起不来的排查过程
  • 代码练习2
  • 2.1.3 第一个工程,点灯!
  • Qt Designer and Python: Build Your GUI
  • 蓝桥杯LQ1044 求完数
  • 消息队列篇--通信协议篇--TCP和UDP(3次握手和4次挥手,与Socket和webSocket的概念区别等)
  • YOLOv9改进,YOLOv9检测头融合ASFF(自适应空间特征融合),全网首发
  • Elastic Agent 对 Kafka 的新输出:数据收集和流式传输的无限可能性
  • 论文速读|Is Cosine-Similarity of Embeddings Really About Similarity?WWW24
  • Midjourney中的强变化、弱变化、局部重绘的本质区别以及其有多逆天的功能
  • 基于 Node.js 的天气查询系统实现(附源码)
  • 时序数据库的使用场景