-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.cpp
107 lines (87 loc) · 2.7 KB
/
demo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// ------------------------------------------------------------------
// Copyright (c) HWater. All rights reserved HWater. if any questions about
// this, please mail to hsqaihkl@163.com
// ------------------------------------------------------------------
// Project Name : RawMultiFrameHDR
// Author : HWater
// Date : 2022-12-18
// Description : Raw HDR demo
// ------------------------------------------------------------------
#include<iostream>
#include<opencv2/opencv.hpp>
cv::Mat ReadRaw(std::string path,int heigh,int width)
{
int size = heigh * width;
cv::Mat raw;
raw.create(cv::Size(width, heigh), CV_16UC1);
ushort* raw_ptr = raw.ptr<ushort>();
FILE* fpsrc = NULL;
if ((fpsrc = fopen(path.data(), "rb")) == NULL) {
printf("Can't open the raw image!\n");
}
fread(raw_ptr, 1, size * sizeof(ushort), fpsrc);
fclose(fpsrc);
return raw;
}
int WriteRaw(std::string path, cv::Mat raw)
{
int height = raw.rows;
int width = raw.cols;
int pixel_num=height*width;
ushort* raw_ptr = raw.ptr<ushort>();
FILE* pFile = fopen(path.data(), "wb");//
if (pFile == nullptr) {
printf("%s file can't open!\n", path.data());
return 0;
}
else {
fwrite(raw_ptr, 2, pixel_num, pFile);
}
fclose(pFile);
return 1;
}
std::string changePathWithPostfix(std::string path, std::string postfix)
{
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char extc[_MAX_EXT];
_splitpath(path.data(), drive, dir, fname, extc);
std::string ext = extc;
int len = path.size();
int ext_len = ext.size();
std::string dst = path;
dst = dst.insert(len - ext_len, postfix);
return dst;
}
void main(int partc, char* partv[])
{
std::string postfix = "_hdr";
std::string path = "..//data//raw//my_house_4640x3472_GBRG.raw";
std::string path_save = "";
path_save = changePathWithPostfix(path, postfix);
int white_value = 1023;
float detapixel = 1.0 / float(white_value);
int heigh=3472;
int width=4640;
float rgain = 1.435f;
float bgain = 2.052f;
float gamma = 2.2;
cv::Mat bayer_image;
if (partc > 1)
{
path = partv[1];
path_save = changePathWithPostfix(path, postfix);
}
bayer_image =ReadRaw(path,heigh,width);
cv::Mat dst_demosaic_image,bay;
bayer_image.convertTo(bay, CV_32FC1, detapixel);
//demosaic
cv::cvtColor(bayer_image, dst_demosaic_image, cv::COLOR_BayerGB2BGR);
dst_demosaic_image.convertTo(dst_demosaic_image, CV_32FC3, detapixel);
//awb
cv::Scalar awb(bgain, 1.0, rgain);
dst_demosaic_image = dst_demosaic_image.mul(awb);
//gamma
cv::pow(dst_demosaic_image, 1.0 / gamma, dst_demosaic_image);
}