C++/OpenCV
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat outImg1, outImg2, outImg3, outImg4, outImg5;
Mat src = imread("E:/1.png", IMREAD_COLOR);
if (src.empty()) {
printf("image is empty!!!");
return -1;
}
cv::Mat kernel;
kernel = cv::getGaborKernel(cv::Size(3, 3), 40, 0, 5, 0.5, 0);
cout << kernel << endl;
std::cout << "kernel: " << kernel << std::endl;
filter2D(src, outImg2, CV_32F, kernel, Point(-1, -1), 0, BORDER_DEFAULT);
imshow("outImg2", outImg2);
SVD svd(kernel);
Mat U = svd.u;
cout << "U = " << endl;
cout << U << endl;
Mat colOne = U.colRange(0, 1).clone();
cout << "colOne = " << endl;
cout << colOne << endl;
Mat V = svd.vt;
cout << "V = " << endl;
cout << V << endl;
Mat rowOne = V.rowRange(0, 1).clone();
cout << "rowOne = " << endl;
cout << rowOne << endl;
Mat D = svd.w;
cout << "D = " << endl;
cout << D << endl;
float Gama =1.89;
Mat resImg_row;
filter2D(src, resImg_row, CV_32F, rowOne* Gama, Point(-1, -1), 0, BORDER_DEFAULT);
Mat resImg_col;
filter2D(resImg_row, resImg_col, CV_32F, colOne, Point(-1, -1), 0, BORDER_DEFAULT);
imshow("resImg_col: ", resImg_col);
cout << " outImg2: "<< outImg2.at<float>(0, 0) << endl;
cout << " resImg_col: " << resImg_col.at<float>(0, 0) << endl;
waitKey(0);
destroyAllWindows();
return 0;
}
Python
import numpy as np
import scipy
import scipy.ndimage
from numpy import linalg as la
from numpy import *
img = [[255, 255, 255],[255, 255,255],[255, 255, 255]]
myl = [[0, -1, 0],[-1, 8, -1],[0, -1, 0]]
# myl = [[4, 0, 5], [0, 0, 5]]
myMat = mat(myl)
U, Sigma, VT = la.svd(myMat)
U, Sigma, VT = np.linalg.svd(myMat)
print("U:", U)
print("Sigma:", Sigma)
print("VT:", VT)
print(U[:, 0, np.newaxis])
print(VT[0,:, np.newaxis])
# x=np.array(U[:,0]).reshape(3,1) #等价于 x=np.arange(0,5)
# y=np.array(VT[0]).reshape(1,3)
# print(x)
# print(y)
# print("Mat:",np.dot(x,y)*Sigma[0])
final_res2 = scipy.ndimage.convolve(img, myl)
print("final res2:",final_res2)
final_res = 0
for i in range(1):
final_res += Sigma[i] * scipy.ndimage.convolve(scipy.ndimage.convolve(img, U[:, i, np.newaxis]),
np.transpose(VT[i, :, np.newaxis]))
print("final res:",final_res)