OpenCV:同态滤波器

2023-12-11

我想使用同态滤波器来处理水下图像。我尝试用互联网上找到的代码对其进行编码,但我总是有一个黑色图像...我尝试标准化我的结果,但没有成功。

这是我的功能:

void HomomorphicFilter::butterworth_homomorphic_filter(Mat &dft_Filter, int D, int n, float high_h_v_TB, float low_h_v_TB)
{
Mat single(dft_Filter.rows, dft_Filter.cols, CV_32F);
Point centre = Point(dft_Filter.rows/2, dft_Filter.cols/2);
double radius;
float upper = (high_h_v_TB * 0.01);
float lower = (low_h_v_TB * 0.01);

//create essentially create a butterworth highpass filter
//with additional scaling and offset
for(int i = 0; i < dft_Filter.rows; i++)
{
    for(int j = 0; j < dft_Filter.cols; j++)
    {
        radius = (double) sqrt(pow((i - centre.x), 2.0) + pow((double) (j - centre.y), 2.0));
        single.at<float>(i,j) =((upper - lower) * (1/(1 + pow((double) (D/radius), (double) (2*n))))) + lower;
    }
}
//normalize(single, single, 0, 1, CV_MINMAX);
//Apply filter
mulSpectrums( dft_Filter, single, dft_Filter, 0);
}

void HomomorphicFilter::Shifting_DFT(Mat &fImage)
{
//For visualization purposes we may also rearrange the quadrants of the result, so that the origin (0,0), corresponds to the image center.
Mat tmp, q0, q1, q2, q3;

/*First crop the image, if it has an odd number of rows or columns.
Operator & bit to bit by -2 (two's complement : -2 = 111111111....10) to eliminate the first bit 2^0 (In case of odd number on row or col, we take the even number in below)*/
fImage = fImage(Rect(0, 0, fImage.cols & -2, fImage.rows & -2));
int cx = fImage.cols/2;
int cy = fImage.rows/2;

/*Rearrange the quadrants of Fourier image so that the origin is at the image center*/
q0 = fImage(Rect(0, 0, cx, cy));
q1 = fImage(Rect(cx, 0, cx, cy));
q2 = fImage(Rect(0, cy, cx, cy));
q3 = fImage(Rect(cx, cy, cx, cy));

/*We reverse each quadrant of the frame with its other quadrant diagonally opposite*/
/*We reverse q0 and q3*/
q0.copyTo(tmp);
q3.copyTo(q0);
tmp.copyTo(q3);

/*We reverse q1 and q2*/
q1.copyTo(tmp);
q2.copyTo(q1);
tmp.copyTo(q2);
}

void HomomorphicFilter::Fourier_Transform(Mat frame_bw, Mat &image_phase, Mat &image_mag)
{
Mat frame_log;
frame_bw.convertTo(frame_log, CV_32F);
/*Take the natural log of the input (compute log(1 + Mag)*/
frame_log += 1;
log( frame_log, frame_log); // log(1 + Mag)

/*2. Expand the image to an optimal size
The performance of the DFT depends of the image size. It tends to be the fastest for image sizes that are multiple of 2, 3 or 5.
We can use the copyMakeBorder() function to expand the borders of an image.*/
Mat padded;
int M = getOptimalDFTSize(frame_log.rows);
int N = getOptimalDFTSize(frame_log.cols);
copyMakeBorder(frame_log, padded, 0, M - frame_log.rows, 0, N - frame_log.cols, BORDER_CONSTANT, Scalar::all(0));

/*Make place for both the complex and real values
The result of the DFT is a complex. Then the result is 2 images (Imaginary + Real), and the frequency domains range is much larger than the spatial one. Therefore we need to store in float !
That's why we will convert our input image "padded" to float and expand it to another channel to hold the complex values.
Planes is an arrow of 2 matrix (planes[0] = Real part, planes[1] = Imaginary part)*/
Mat image_planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat image_complex;
/*Creates one multichannel array out of several single-channel ones.*/
merge(image_planes, 2, image_complex);

/*Make the DFT
The result of thee DFT is a complex image : "image_complex"*/
dft(image_complex, image_complex);

/***************************/
//Create spectrum magnitude//
/***************************/
/*Transform the real and complex values to magnitude
NB: We separe Real part to Imaginary part*/
split(image_complex, image_planes);
//Starting with this part we have the real part of the image in planes[0] and the imaginary in planes[1]
phase(image_planes[0], image_planes[1], image_phase);
magnitude(image_planes[0], image_planes[1], image_mag);
}

void HomomorphicFilter::Inv_Fourier_Transform(Mat image_phase, Mat image_mag, Mat &inverseTransform)
{
/*Calculates x and y coordinates of 2D vectors from their magnitude and angle.*/
Mat result_planes[2];
polarToCart(image_mag, image_phase, result_planes[0], result_planes[1]);

/*Creates one multichannel array out of several single-channel ones.*/
Mat result_complex;
merge(result_planes, 2, result_complex);

/*Make the IDFT*/
dft(result_complex, inverseTransform, DFT_INVERSE|DFT_REAL_OUTPUT);

/*Take the exponential*/
exp(inverseTransform, inverseTransform);
}

这是我的主要代码:

    /**************************/
    /****Homomorphic filter****/
    /**************************/
    /**********************************************/
    //Getting the frequency and magnitude of image//
    /**********************************************/
    Mat image_phase, image_mag;
    HomomorphicFilter().Fourier_Transform(frame_bw, image_phase, image_mag);
    /******************/
    //Shifting the DFT//
    /******************/
    HomomorphicFilter().Shifting_DFT(image_mag);
    /********************************/
    //Butterworth homomorphic filter//
    /********************************/
    int high_h_v_TB = 101;
    int low_h_v_TB = 99;
    int D = 10;// radius of band pass filter parameter
    int order = 2;// order of band pass filter parameter
    HomomorphicFilter().butterworth_homomorphic_filter(image_mag, D, order, high_h_v_TB, low_h_v_TB);
    /******************/
    //Shifting the DFT//
    /******************/
    HomomorphicFilter().Shifting_DFT(image_mag);
    /*******************************/
    //Inv Discret Fourier Transform//
    /*******************************/
    Mat inverseTransform;
    HomomorphicFilter().Inv_Fourier_Transform(image_phase, image_mag, inverseTransform);
    imshow("Result", inverseTransform);

如果有人可以解释我的错误,我将不胜感激:)。谢谢你,并对我糟糕的英语感到抱歉。

编辑:现在,我有一些东西,但它并不完美......我修改了代码中的两件事。 我在 dft 之后应用了 log(mag + 1),而不是在输入图像上。 我在 idft 之后删除了 exp() 。

这是结果(我只能发布 2 个链接...):

my input image : http://i.imgur.com/Kw0Wdo4.png final result : http://i.imgur.com/7AX9j5u.png

在看过几个主题后,我在巴特沃斯滤波器上和 dft/shifting 后的幅度上发现了类似的结果。 不幸的是,我的最终结果不是很好。为什么我有这么多“噪音”?


当更换相机导致图像变暗时,我正在使用这种方法来平衡照明!

我尝试对频率进行 FFT 来过滤图像!它可以工作。但是花费了太多时间。(2750 * 3680RGB图像)。所以我在空间域中进行。

这是我的代码!

//IplImage *imgSrcI=cvLoadImage("E:\\lean.jpg",-1);
Mat imgSrcM(imgSrc,true);
Mat imgDstM;

Mat imgGray;
Mat imgHls;
vector<Mat> vHls;

Mat imgTemp1=Mat::zeros(imgSrcM.size(),CV_64FC1);
Mat imgTemp2=Mat::zeros(imgSrcM.size(),CV_64FC1);

if(imgSrcM.channels()==1)
{
    imgGray=imgSrcM.clone();
}
else if (imgSrcM.channels()==3)
{
    cvtColor(imgSrcM, imgHls, CV_BGR2HLS);
    split(imgHls, vHls);
    imgGray=vHls.at(1);
}
else
{
    return -1;
}
imgGray.convertTo(imgTemp1,CV_64FC1);
imgTemp1=imgTemp1+0.0001;
log(imgTemp1,imgTemp1);

GaussianBlur(imgTemp1, imgTemp2, Size(21, 21), 0.1, 0.1, BORDER_DEFAULT);//imgTemp2是低通滤波的结果
imgTemp1 = (imgTemp1 - imgTemp2);//imgTemp1是对数减低通的高通
addWeighted(imgTemp2, 0.7, imgTemp1, 1.4, 1, imgTemp1, -1);//imgTemp1是压制低频增强高频的结构

exp(imgTemp1,imgTemp1);
normalize(imgTemp1,imgTemp1,0,1,NORM_MINMAX);
imgTemp1=imgTemp1*255;

imgTemp1.convertTo(imgGray, CV_8UC1);

//imwrite("E:\\leanImgGray.jpg",imgGray);
if (imgSrcM.channels()==3)
{
    vHls.at(1)=imgGray;
    merge(vHls,imgHls);
    cvtColor(imgHls, imgDstM, CV_HLS2BGR);

}
else if (imgSrcM.channels()==1)
{
    imgDstM=imgGray.clone();
}

cvCopy(&(IplImage)imgDstM,imgDst);
//cvShowImage("jpg",imgDst);

return 0;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OpenCV:同态滤波器 的相关文章

  • 多个源的 makefile

    在学习 make 文件时 我试图为多个源目录编写一个 make 文件 似乎我在某个地方错了 这是我的代码结构 directory common fun2 c inc fun h src fun1 c main c 这是我的生成文件 CC c
  • 添加 Nullable int 时保持 null?

    我想添加可为空的int 并保留null当所有值都是null 我想要这个结果 1 2 3 1 null 1 null null null O null 0 问题是 如果我将一个值与 null 相加 结果为 null int i1 1 int
  • Poco c++Net:Http 从响应中获取标头

    我使用 POCO C Net 库进行 http 我想尝试制定持久缓存策略 首先 我认为我需要从缓存标头中获取过期时间 并与缓存值进行交叉检查 如果我错了 请告诉我 那么我如何从中提取缓存头httpResponse 我已经看到你可以用 Jav
  • 检测wlan是否关闭

    任何人都可以给我一个提示 如何在 Windows Phone 上以编程方式检测 C 8 1 应用程序 不是 8 0 是否启用 禁用 WLAN 我不想更改这些设置 只是需要知道 该解决方案是一个 Windows 8 1 通用应用程序 Wind
  • CSharpRepl emacs 集成?

    我碰巧知道莫诺CSharpRepl http www mono project com CsharpRepl 是否有 emacs csharp 模式使用它在一个窗口中运行 REPL 并像 python 模式一样在另一个窗口中编译 运行 C
  • 为什么'enable_if'不能用于禁用这里声明

    include
  • 将表(行)与 OpenXML SDK 2.5 保持在一起

    我想在 Word 文档中生成多个表 每行 2 行 但我想将这两行保留在一起 如果可能的话 new KeepNext 第一行不起作用 new KeepNext 第一行的最后一段不起作用 new CantSplit 放在桌子上不起作用 在所有情
  • MFC:如何设置CEdit框的焦点?

    我正在开发我的第一个简单的 MFC 项目 但我正在努力解决一个问题 想要设置所有的焦点CEdit其中一个对话框中的框 我的想法是 当打开对话框时 焦点位于第一个编辑框上 然后使用 选项卡 在它们之间交换 我看到了方法SetFocus 但我无
  • UI 函数在快速事件完成之前触发

    我有一个停靠在 Silverlight 应用程序中的 Web 浏览器框架 有时会在其上弹出全窗口 XAML Silverlight UI 元素 我已经或多或少修复了一个老问题 即 Web 框架的内容似乎与 Silverlight 内容不能很
  • 搜索实体的所有字段

    我正在尝试在客户数据库上实现 多功能框 类型的搜索 其中单个查询应尝试匹配客户的任何属性 这是一些示例数据来说明我想要实现的目标 FirstName LastName PhoneNumber ZipCode Mary Jane 12345
  • 使用具有抗锯齿功能的 C# 更改抗锯齿图像的背景颜色

    我有一个图像需要更改背景颜色 例如 将下面示例图像的背景更改为蓝色 然而 图像是抗锯齿的 所以我不能简单地用不同的颜色替换背景颜色 我尝试过的一种方法是创建第二个图像 仅作为背景 并更改其颜色并将两个图像合并为一个图像 但是这不起作用 因为
  • .NET 4 的条件编译[重复]

    这个问题在这里已经有答案了 可能的重复 条件编译和框架目标 https stackoverflow com questions 2923210 c sharp conditional compilation and framework ta
  • 使用 jQuery 从 ASP.Net JSON 服务获取数据

    我正在尝试调用 Google 地图地理编码 API 从纬度 经度对中获取格式化的地址 然后将其记录到控制台 我正在尝试获取为给定位置返回的第一个 formatted address 项目 我很简单无法从 JSON 中提取该项目 我不知道为什
  • 如何调试 .NET 运行时中的内部错误?

    我正在尝试调试一些处理大文件的工作 代码本身works 但 NET 运行时本身会报告零星错误 对于上下文 这里的处理是一个 1 5GB 文件 仅加载到内存中一次 在循环中处理和释放 故意尝试重现此否则不可预测的错误 我的测试片段基本上是 t
  • 需要提取字符串中点后的最后一个数字,如“7.8.9.1.5.1.100”

    我需要提取 C 字符串中最后一个点后面的最后一个数字 例如 7 8 9 1 5 1 100 并将其存储在整数中 Added 该字符串也可以是 7 8 9 1 5 1 1 或 7 8 9 1 5 1 0 我还想验证它在最后一个点之前恰好是 7
  • 通过 Tab 键浏览 XML 文档字段

    In VB NET you can move through the fields in the XML member documentation with the Tab key 这在 C 中不起作用 还有其他方法吗 除了用鼠标将光标放在
  • INotifyPropertyChanged 和 propertyName

    我一直不确定它的含义propertyName实施时INotifyPropertyChanged 所以一般来说你实现INotifyPropertyChanged as public class Data INotifyPropertyChan
  • 如何将 SQL“LIKE”与 LINQ to Entities 结合使用?

    我有一个文本框 允许用户指定搜索字符串 包括通配符 例如 Joh Johnson mit ack on 在使用 LINQ to Entities 之前 我有一个存储过程 该存储过程将该字符串作为参数并执行以下操作 SELECT FROM T
  • 如何使用placement new重新初始化该字段?

    我的课程包含字段 private OrderUpdate curOrderUpdate 我一遍又一遍地使用它 经常需要重新初始化 for int i 0 i lt entries size i auto entry entries i ne
  • 为什么匹配模板类上的部分类模板特化与没有模板匹配的另一个部分特化不明确?

    这个问题可能很难用标题中的句子来描述 但这里有一个最小的例子 include

随机推荐

  • 检测重复文件的最快算法

    在我的 2 TB HDD 存储图像中查找重复项的过程中 我对 fslint 和 fslint gui 工具的长时间运行时间感到惊讶 所以我分析了核心工具的内部结构findup它是使用超长管道作为编写良好且记录良好的 shell 脚本实现的
  • 当logstash过滤器获取eof时如何返回终端?

    现在 当logstash过滤器获取eof时 似乎logstash过滤器正在运行 但文件没有更多日志可输出到elasticsearch索引 当文件末尾 eof 时 我怎样才能退出logstash过滤器来执行其他任务 我在logstash过滤器
  • 如何在 Vaadin 视图中的网格/表格中设置单元格背景颜色?

    我正在使用 Vaadin 我想为网格 表格中的特定单元格设置背景颜色 或者如果无法为特定单元格设置背景颜色 我想至少为网格 表格中的特定单元格设置字体颜色 我有网格 表格的 TableView 代码如下 package com tradin
  • 使用 ServicePartitionClient 对服务结构 StatelessService 进行负载平衡

    我正在 Service Fabric 集群上实现 API 网关 其中 API 网关服务是将外部 HTTP 请求路由到集群中运行的一组辅助服务的公共端点 对于网关和内部服务之间的服务间通信 我们使用 ServicePartitionClien
  • 如何从 subprocess.Popen() 获取输出。 proc.stdout.readline() 块,没有数据打印出来

    我想要执行 Test Pipe py 的输出 我尝试在 Linux 上执行以下代码 但它不起作用 测试管道 py import time while True print Someting time sleep 1 来电者 py impor
  • ActiveRecord 的哈希数组

    ActiveRecord Rails 4 0 支持 PostgreSQL Hstore 和数组数据类型 因此哈希数组理论上是可能的 但我的实现抛出 PG InvalidTextRepresentation ERROR malformed a
  • 无法更新 Matlab 中的类定义

    我在使用 Matlab 时遇到了一个令人恼火的问题 并且先前的回答不幸的是 显然同样的问题对我没有帮助 我很抱歉这个问题相当长 你需要相当多的信息来重现这个问题 我试图尽可能地修剪它 问题是这样的 无论我做什么 在我使用过一个类之后我都不能
  • JFrame - 使用 JComponent 和 MouseListener 进行鼠标单击

    有2个班级 public class MainClass public static void main String args JFrame frame new JFrame Component mouseClick new MyComp
  • 为什么不完整的类型不能转换为 void?

    为什么下面的代码会出现以下错误 为什么类型需要完整才能转换为void struct Incomplete class Class virtual void foo Incomplete incomplete void incomplete
  • git push heroku master - 没有错误消息,但更改未显示在网络应用程序上

    安装 devise gem 在我的网络应用程序上创建用户后 我的更改没有出现在实时应用程序上 我相信我在终端中运行了正确的代码 我正在学习一个月的 Rails 课程 git add git commit am message git pus
  • 如何使 TabBar 在 TabWidget 上水平居中

    这是一个简短的例子 可以解释我想要什么 代替 TAB 1 TAB 2 Tab 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX i desire XXXXXXXXXXXXXXXXXX TAB 1 TAB 2 Ta
  • 实体框架 ObjectContext -> 对本机 DBMS 的原始 SQL 调用

    我有一个使用 ADO NET 实体框架 VS2008 版本 而不是更新 更酷的版本 的应用程序 我需要能够调用底层 DBMS 它是 postgres 以便调用该实体的一些 SQL框架不支持 有没有办法从实体框架 ObjectContext
  • SQL 将处理后的 id 添加到用逗号分隔的单个单元格

    我有以下 sql 查询来了解它的作用 请阅读下面的描述 select catalogid numitems allitems numitems ignoreditems from select i catalogid sum case wh
  • 按标记名过滤 logcat 中的输出

    我试图按标签名称过滤来自真实设备 不是模拟器 的 logcat 输出 但我收到的所有消息都是垃圾邮件 我只想从浏览器读取消息 应该类似于 浏览器 或 网络工具包 但是这不起作用 这是我得到的 用这个 adb logcat s TAGNAME
  • Azure 长响应时间瓶颈?

    如何诊断 Azure 中的性能 响应时间 瓶颈 我在 Azure 上有一个 NET Core 网站 其中包含一项 Web 应用服务和一个 SQL 数据库 我已经设置了负载测试并通过云部署它来访问网站 负载测试代理的配置属性是 4 cores
  • 范围报告未给出有关并行执行的正确报告

    记者类 Java package POM Classes import com aventstack extentreports AnalysisStrategy import com aventstack extentreports Ex
  • 如何将相对 PIDL 转换为绝对 PIDL?

    我想用SHGetFileInfo要获取Windows控制面板图标 我使用Shell来获取Windows控制面板 代码 var psfDeskTop IShellFolder psfWork IShellFolder pidworkDir P
  • 如何确保 TFS 客户端安装了最新版本的自定义签入策略?

    我们实施了自定义签入策略 并为我们的一个 TFS 项目选择了它 如果 TFS 客户端未安装该策略 TFS 将显示一条错误消息以及安装说明 这一切都很好 但是 当出现新版本的入住政策时 我们会遇到问题 TFS 似乎只强制执行策略名称 而不强制
  • uitextfield隐藏键盘?

    在 iPhone 上 当我按下uitextfield我不想弹出键盘 我想要同样的行为 只是根本没有键盘 当用户按下时如何隐藏 关闭键盘uitextfield 如果您想要完全相同的行为 无需键盘 请尝试 textfield inputView
  • OpenCV:同态滤波器

    我想使用同态滤波器来处理水下图像 我尝试用互联网上找到的代码对其进行编码 但我总是有一个黑色图像 我尝试标准化我的结果 但没有成功 这是我的功能 void HomomorphicFilter butterworth homomorphic