Skip to content

Commit 4d1e59c

Browse files
committed
installing opencv
1 parent 2e80fe5 commit 4d1e59c

File tree

7 files changed

+78
-7
lines changed

7 files changed

+78
-7
lines changed

.github/workflows/BuildRelight.yml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ jobs:
3434
New-Item -ItemType directory -Path certificate
3535
Set-Content -Path certificate\certificate.txt -Value '${{ secrets.WIN_CERTIFICATE }}'
3636
certutil -decode certificate\certificate.txt certificate\certificate.pfx
37+
- name: Install opencv
38+
uses: Dovyski/[email protected]
39+
with:
40+
opencv-version: '4.0.0'
3741
- name: Install Qt
3842
uses: jurplel/install-qt-action@v4
3943
with:

align/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ endif()
1717

1818
SET(SOURCES
1919
main.cpp
20+
imagealignment.cpp
2021
)
2122

22-
23+
find_package(OpenCV REQUIRED)
24+
message(STATUS ${OpenCV_LIBS})
2325

2426
add_executable(align ${HEADERS} ${SOURCES})
2527
target_include_directories(
@@ -30,6 +32,7 @@ target_include_directories(
3032
target_link_libraries(
3133
align PUBLIC
3234
${JPEG_LIBRARIES}
35+
opencv_core opencv_imgcodecs opencv_imgproc opencv_video
3336
${RELIGHT_QT}::Core
3437
${RELIGHT_QT}::Gui
3538
)

align/imagealignment.cpp

+63-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,64 @@ void ImageAlignment::alignSamples(bool useECC) {
2121
}
2222
}
2323

24+
25+
double mutualInformation(const cv::Mat& a, const cv::Mat& b) {
26+
std::vector<int> histo(256*256, 0);
27+
std::vector<int> aprob(256, 0);
28+
std::vector<int> bprob(256, 0);
29+
int width = a.cols;
30+
int height = a.rows;
31+
//x and y refer to the image pixels!
32+
for(int y = 0; y < height; y++) {
33+
for(int x = 0; x < width; x++) {
34+
int ca = a.at<unsigned char>(y, x);
35+
int cb = b.at<unsigned char>(y, x);
36+
histo[ca + 256*cb]++;
37+
aprob[ca]++;
38+
bprob[cb]++;
39+
}
40+
}
41+
// w * h vettore, trova min e max, scala tra 0 e 1 e dopo lo metti su una qimg
42+
//
43+
double tot = height*width;
44+
double info = 0.0;
45+
46+
for(int y = 0; y < 256; y++) {
47+
for(int x = 0; x < 256; x++) {
48+
double p = histo[x + 256*y]/tot;
49+
if(p == 0) continue;
50+
double pa = aprob[x]/tot;
51+
double pb = bprob[y]/tot;
52+
info += p * log(p/(pa*pb));
53+
}
54+
}
55+
return info;
56+
}
57+
58+
59+
void ImageAlignment::testAlign() {
60+
int k = 10;
61+
int w = samples[0].cols;
62+
int h = samples[0].rows;
63+
cv::Mat a = samples[0](cv::Rect(k, k, w-2*k, h-2*k));
64+
for(int i = 0; i < samples.size(); i++) {
65+
cv::Mat result(2*k+1, 2*k+1, CV_32F);
66+
for(int dy = -k; dy <= k; dy++) {
67+
for(int dx = -k; dx <= k; dx++) {
68+
cv::Mat b = samples[i](cv::Rect(k + dx, k+dy, w-2*k, h-2*k));
69+
float my = mutualInformation(a, b);
70+
//float my = computeECCValue(a, b);
71+
result.at<float>(dy +k, dx+k) = my;
72+
}
73+
}
74+
cv::Mat normalizedMat, uint8Mat;
75+
cv::normalize(result, normalizedMat, 0, 255, cv::NORM_MINMAX);
76+
normalizedMat.convertTo(uint8Mat, CV_8U);
77+
cv::imwrite(std::to_string(i) + ".png", uint8Mat);
78+
}
79+
}
80+
81+
2482
double ImageAlignment::computeECCValue(const cv::Mat& src, const cv::Mat& ref) {
2583
cv::Mat srcFloat, refFloat;
2684
src.convertTo(srcFloat, CV_32F);
@@ -44,14 +102,16 @@ cv::Mat ImageAlignment::computeECC(const cv::Mat& src, const cv::Mat& ref) {
44102
}
45103

46104
double ImageAlignment::computeMutualInformationValue(const cv::Mat& src, const cv::Mat& ref) {
105+
return mutualInformation(src, ref);
47106
const int histSize = 256;
48107
float range[] = { 0, 256 };
49108
const float* histRange = { range };
50109

51110
cv::Mat histSrc, histRef, jointHist;
52-
cv::calcHist(&src, 1, 0, cv::Mat(), histSrc, 1, &histSize, &histRange);
53-
cv::calcHist(&ref, 1, 0, cv::Mat(), histRef, 1, &histSize, &histRange);
54-
cv::calcHist(&src, 1, 0, ref, jointHist, 2, &histSize, &histRange);
111+
int channels[] = {0};
112+
cv::calcHist(&src, 1, channels, cv::Mat(), histSrc, 1, &histSize, &histRange);
113+
cv::calcHist(&ref, 1, channels, cv::Mat(), histRef, 1, &histSize, &histRange);
114+
cv::calcHist(&src, 1, channels, ref, jointHist, 2, &histSize, &histRange);
55115

56116
cv::normalize(histSrc, histSrc, 1, 0, cv::NORM_L1);
57117
cv::normalize(histRef, histRef, 1, 0, cv::NORM_L1);

align/imagealignment.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ImageAlignment {
1818
ImageAlignment(const cv::Rect2f& region_): region(region_) {}
1919

2020
void alignSamples(bool useECC = true);
21+
void testAlign();
2122

2223
private:
2324
double computeECCValue(const cv::Mat& src, const cv::Mat& ref);

align/main.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ int main(int argc, char *argv[]) {
258258
samples.push_back(img(region));
259259
original_offsets.push_back(cv::Point2f(-dx, -dy));
260260
}
261-
262-
alignment.alignSamples(true);
261+
alignment.testAlign();
262+
return 0;
263+
alignment.alignSamples(false);
263264
for(int i = 0; i < original_offsets.size(); i++) {
264265
auto o = alignment.offsets[i];
265266
if(o.x == 0 && o.y == 0 && i != 0) {

build_scripts/Linux/0_setup_env.sh

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ sudo apt-get install -y cmake ninja-build patchelf libjpeg-dev libeigen3-dev
2929
# qt dependencies (for deployment)
3030
sudo apt-get install -y libxcb-cursor0
3131

32+
#sudo apt-get install libopencv-dev
33+
3234
if [ "$DONT_INSTALL_QT" = false ] ; then
3335
echo "=== installing qt packages..."
3436
sudo apt-get install -y qt5-default qttools5-dev-tools qtdeclarative5-dev

build_scripts/macOS/0_setup_env.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ esac
2323
done
2424

2525

26-
brew install coreutils cmake ninja libomp eigen libjpeg libtiff
26+
brew install coreutils cmake ninja libomp eigen libjpeg libtiff #opencv
2727

2828
npm install -g appdmg
2929

0 commit comments

Comments
 (0)