FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OpenSplat/cv_utils.cpp at OpenSplatLibrary · NewChromantics/OpenSplat · GitHub
NewChromantics
/
OpenSplat
Public
forked from
WebODM/OpenSplat
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OpenSplat
/
cv_utils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
82 lines (66 loc) · 2.27 KB
Breadcrumbs
OpenSplat
/
cv_utils.cpp
Copy path
File metadata and controls
82 lines (66 loc) · 2.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
cv_utils.hpp
"
cv::Mat
imreadRGB
(
const
std::string &filename){
cv::Mat cImg =
cv::imread
(filename);
if
(cImg.
empty
())
{
std::stringstream Error;
Error <<
"
Cannot read
"
<< filename << std::endl
<<
"
Make sure the path to your images is correct
"
<< std::endl;
throw
std::runtime_error
(Error.
str
());
}
cv::cvtColor
(cImg, cImg, cv::
COLOR_BGR2RGB
);
return
cImg;
}
void
imwriteRGB
(
const
std::string &filename,
const
cv::Mat &image){
cv::Mat rgb;
cv::cvtColor
(image, rgb, cv::
COLOR_RGB2BGR
);
cv::imwrite
(filename, rgb);
}
cv::Mat
floatNxNtensorToMat
(
const
torch::Tensor &t){
return
cv::Mat
(t.
size
(
0
), t.
size
(
1
),
CV_32F
, t.
data_ptr
());
}
torch::Tensor
floatNxNMatToTensor
(
const
cv::Mat &m){
return
torch::from_blob
(m.
data
, { m.
rows
, m.
cols
}, torch::
kFloat32
).
clone
();
}
cv::Mat
tensorToImage
(
const
torch::Tensor &t)
{
//
if ( t.dim() <= 1 )
int
h = t.
sizes
()[
0
];
int
w = t.
sizes
()[
1
];
int
c = t.
sizes
()[
2
];
int
type =
CV_8UC3
;
if
(c !=
3
)
{
std::stringstream Error;
Error << __FUNCTION__ <<
"
Only images with 3 channels are supported (this:
"
<< w <<
"
x
"
<< h <<
"
x
"
<< c <<
"
)
"
;
throw
std::runtime_error
(Error.
str
());
}
cv::Mat
image
(h, w, type);
torch::Tensor scaledTensor = (t *
255.0
).
toType
(torch::
kU8
);
uint8_t
* dataPtr =
static_cast
<
uint8_t
*>(scaledTensor.
data_ptr
());
std::copy
(dataPtr, dataPtr + (w * h * c), image.
data
);
return
image;
}
void
tensorToImage
(
const
torch::Tensor &t,std::function<
void
(
const
cv::Mat&)> OnImage)
{
int
h = t.
sizes
()[
0
];
int
w = t.
sizes
()[
1
];
int
c = t.
sizes
()[
2
];
int
type =
CV_8UC3
;
if
(c !=
3
)
{
std::stringstream Error;
Error << __FUNCTION__ <<
"
Only images with 3 channels are supported (this:
"
<< w <<
"
x
"
<< h <<
"
x
"
<< c <<
"
)
"
;
throw
std::runtime_error
(Error.
str
());
}
torch::Tensor scaledTensor = (t *
255.0
).
toType
(torch::
kU8
);
uint8_t
* dataPtr =
static_cast
<
uint8_t
*>(scaledTensor.
data_ptr
());
cv::Mat
image
(h, w, type, dataPtr);
//
std::copy(dataPtr, dataPtr + (w * h * c), image.data);
OnImage
(image);
}
torch::Tensor
imageToTensor
(
const
cv::Mat &image)
{
torch::Tensor img =
torch::from_blob
(image.
data
, { image.
rows
, image.
cols
, image.
dims
+
1
}, torch::
kU8
);
return
(img.
toType
(torch::
kFloat32
) /
255
.
0f
);
}
Back
|
FazBrowse Home
|
New Git URL