opencv系列學習 2

2021-10-04 02:17:28 字數 4483 閱讀 3752

傅利葉變換會將影象分解成其正弦和余弦分量。換句話說,它將影象從空間域轉換到頻率域。這個想法是,任何函式都可以用無限的正弦和余弦函式之和精確地近似。傅利葉變換是一種方法。數學上,二維影象的傅利葉變換為:

#include "opencv2/core.hpp"

#include "opencv2/imgproc.hpp"

#include "opencv2/imgcodecs.hpp"

#include "opencv2/highgui.hpp"

#include using namespace cv;

using namespace std;

static void help(char ** ar**)

int main(int argc, char ** ar**)

mat padded; //expand input image to optimal size

int m = getoptimaldftsize( i.rows );

int n = getoptimaldftsize( i.cols ); // on the border add zero values

copymakeborder(i, padded, 0, m - i.rows, 0, n - i.cols, border_constant, scalar::all(0));

mat planes = ;

mat complexi;

merge(planes, 2, complexi); // add to the expanded another plane with zeros

dft(complexi, complexi); // this way the result may fit in the source matrix

// compute the magnitude and switch to logarithmic scale

// => log(1 + sqrt(re(dft(i))^2 + im(dft(i))^2))

split(complexi, planes); // planes[0] = re(dft(i), planes[1] = im(dft(i))

magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude

mat magi = planes[0];

magi += scalar::all(1); // switch to logarithmic scale

log(magi, magi);

// crop the spectrum, if it has an odd number of rows or columns

magi = magi(rect(0, 0, magi.cols & -2, magi.rows & -2));

// rearrange the quadrants of fourier image so that the origin is at the image center

int cx = magi.cols/2;

int cy = magi.rows/2;

mat q0(magi, rect(0, 0, cx, cy)); // top-left - create a roi per quadrant

mat q1(magi, rect(cx, 0, cx, cy)); // top-right

mat q2(magi, rect(0, cy, cx, cy)); // bottom-left

mat q3(magi, rect(cx, cy, cx, cy)); // bottom-right

mat tmp; // swap quadrants (top-left with bottom-right)

q0.copyto(tmp);

q3.copyto(q0);

tmp.copyto(q3);

q1.copyto(tmp); // swap quadrant (top-right with bottom-left)

q2.copyto(q1);

tmp.copyto(q2);

normalize(magi, magi, 0, 1, norm_minmax); // transform the matrix with float values into a

// viewable image form (float between values 0 and 1).

imshow("input image" , i ); // show the result

imshow("spectrum magnitude", magi);

waitkey();

return exit_success;

}

mat padded;                            //expand input image to optimal size

int m = getoptimaldftsize( i.rows );

int n = getoptimaldftsize( i.cols ); // on the border add zero values

copymakeborder(i, padded, 0, m - i.rows, 0, n - i.cols, border_constant, scalar::all(0));

mat planes = ;

mat complexi;

merge(planes, 2, complexi); // add to the expanded another plane with zeros

dft(complexi, complexi);            // this way the result may fit in the source matrix
split(complexi, planes);                   // planes[0] = re(dft(i), planes[1] = im(dft(i))

magnitude(planes[0], planes[1], planes[0]);// planes[0] = magnitude

mat magi = planes[0];

magi += scalar::all(1);                    // switch to logarithmic scale

log(magi, magi);

// crop the spectrum, if it has an odd number of rows or columns

magi = magi(rect(0, 0, magi.cols & -2, magi.rows & -2));

// rearrange the quadrants of fourier image so that the origin is at the image center

int cx = magi.cols/2;

int cy = magi.rows/2;

mat q0(magi, rect(0, 0, cx, cy)); // top-left - create a roi per quadrant

mat q1(magi, rect(cx, 0, cx, cy)); // top-right

mat q2(magi, rect(0, cy, cx, cy)); // bottom-left

mat q3(magi, rect(cx, cy, cx, cy)); // bottom-right

mat tmp; // swap quadrants (top-left with bottom-right)

q0.copyto(tmp);

q3.copyto(q0);

tmp.copyto(q3);

q1.copyto(tmp); // swap quadrant (top-right with bottom-left)

q2.copyto(q1);

tmp.copyto(q2);

normalize(magi, magi, 0, 1, norm_minmax); // transform the matrix with float values into a

// viewable image form (float between values 0 and 1).

Opencv2系列學習筆記12 檢測fast特徵

一 fast特徵定義什麼是角點 這次的定義基於假定特徵點周圍的影象強度,通過檢查候選畫素周圍一圈畫素來決定是否接受乙個特徵點。與中心點差異較大的畫素如果組成連續的圓弧,並且弧長大於原周長的 3 4,那麼我們認為找到了乙個特徵點。二 加速技巧 首先測試圓上被90度分割的四個點 頂部,底部,左側及右側 ...

Opencv2系列學習筆記11 霍爾夫變換

本節主要介紹如何用hough變換檢測直線和圓 一 hough變換檢測直線 1 原始hough變換 思想 先求出影象中每點的極座標方程 如下,相交於一點的極座標曲線的個數大於最小投票數,則將該點所對應的 p,r0 放入vector 中,即得到一條直線,lines 中儲存的是極座標方程的引數 注意 ho...

Python Web系列學習2 Django

1 django admin django專案管理工具,建立乙個django專案用 django admin startproject 生成的站點目錄結構為 3 在完成django專案和應用的建立後,開始編寫 的應用 4 python manage py runserver 0.0.0.0 8001...