Skip to content

Commit 2bf535f

Browse files
committed
feat(samples): add OpenCVDemo
1 parent d2c2c84 commit 2bf535f

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

samples/OpenCVDemo/.clang-format

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Generated from CLion C/C++ Code Style settings
2+
BasedOnStyle: LLVM
3+
AccessModifierOffset: -1
4+
AlignAfterOpenBracket: Align
5+
AlignConsecutiveAssignments: None
6+
AlignOperands: DontAlign
7+
AllowAllArgumentsOnNextLine: false
8+
AllowAllConstructorInitializersOnNextLine: false
9+
AllowAllParametersOfDeclarationOnNextLine: false
10+
AllowShortBlocksOnASingleLine: Always
11+
AllowShortCaseLabelsOnASingleLine: true
12+
AllowShortFunctionsOnASingleLine: All
13+
AllowShortIfStatementsOnASingleLine: Always
14+
AllowShortLambdasOnASingleLine: All
15+
AllowShortLoopsOnASingleLine: true
16+
AlwaysBreakAfterReturnType: None
17+
AlwaysBreakTemplateDeclarations: Yes
18+
BreakBeforeBraces: Custom
19+
BraceWrapping:
20+
AfterCaseLabel: false
21+
AfterClass: false
22+
AfterControlStatement: Never
23+
AfterEnum: false
24+
AfterFunction: false
25+
AfterNamespace: false
26+
AfterUnion: false
27+
BeforeCatch: false
28+
BeforeElse: false
29+
IndentBraces: false
30+
SplitEmptyFunction: false
31+
SplitEmptyRecord: true
32+
BreakBeforeBinaryOperators: NonAssignment
33+
BreakBeforeTernaryOperators: true
34+
BreakConstructorInitializers: BeforeColon
35+
BreakInheritanceList: BeforeColon
36+
ColumnLimit: 0
37+
CompactNamespaces: false
38+
ContinuationIndentWidth: 4
39+
IndentCaseLabels: true
40+
IndentPPDirectives: None
41+
IndentWidth: 2
42+
KeepEmptyLinesAtTheStartOfBlocks: true
43+
MaxEmptyLinesToKeep: 1
44+
NamespaceIndentation: None
45+
ObjCSpaceAfterProperty: false
46+
ObjCSpaceBeforeProtocolList: false
47+
PointerAlignment: Right
48+
ReflowComments: false
49+
SpaceAfterCStyleCast: true
50+
SpaceAfterLogicalNot: false
51+
SpaceAfterTemplateKeyword: false
52+
SpaceBeforeAssignmentOperators: true
53+
SpaceBeforeCpp11BracedList: false
54+
SpaceBeforeCtorInitializerColon: true
55+
SpaceBeforeInheritanceColon: true
56+
SpaceBeforeParens: ControlStatements
57+
SpaceBeforeRangeBasedForLoopColon: false
58+
SpaceInEmptyParentheses: false
59+
SpacesBeforeTrailingComments: 0
60+
SpacesInAngles: false
61+
SpacesInCStyleCastParentheses: false
62+
SpacesInContainerLiterals: false
63+
SpacesInParentheses: false
64+
SpacesInSquareBrackets: false
65+
TabWidth: 4
66+
UseTab: Never

samples/OpenCVDemo/3rdparty/opencv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/home/zj/opencv/opencv-4.9.0/install_Release

samples/OpenCVDemo/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.24)
2+
project(OpenCVDemo)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
# set opencv
7+
get_filename_component(ABSOLUTE_OpenCV_DIR ./3rdparty/opencv ABSOLUTE)
8+
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
9+
set(OpenCV_DIR ${ABSOLUTE_OpenCV_DIR}/lib/cmake/opencv4)
10+
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
11+
set(OpenCV_DIR ${ABSOLUTE_OpenCV_DIR})
12+
ENDIF ()
13+
find_package(OpenCV REQUIRED)
14+
15+
MESSAGE("OpenCV version: ${OpenCV_VERSION}")
16+
MESSAGE("OpenCV OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
17+
MESSAGE("OpenCV OpenCV_LIBS: ${OpenCV_LIBS}")
18+
19+
20+
add_executable(OpenCVDemo main.cpp)
21+
target_link_libraries(OpenCVDemo ${OpenCV_LIBS})

samples/OpenCVDemo/cropped_lena.jpg

32.6 KB
Loading

samples/OpenCVDemo/lena.jpg

89.7 KB
Loading

samples/OpenCVDemo/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
3+
#include "opencv2/opencv.hpp"
4+
5+
int main() {
6+
// 读取图片
7+
cv::Mat image = cv::imread("../lena.jpg");
8+
if (image.empty()) {
9+
std::cout << "无法读取图片,请确保文件路径正确" << std::endl;
10+
return -1;
11+
}
12+
13+
// 获取图片的尺寸
14+
int width = image.cols;
15+
int height = image.rows;
16+
17+
// 计算中心裁剪的区域
18+
int x = width / 4; // 裁剪区域的左上角 x 坐标
19+
int y = height / 4; // 裁剪区域的左上角 y 坐标
20+
int cropWidth = width / 2; // 裁剪区域的宽度
21+
int cropHeight = height / 2;// 裁剪区域的高度
22+
23+
// 裁剪图片
24+
cv::Rect cropRegion(x, y, cropWidth, cropHeight);
25+
cv::Mat croppedImage = image(cropRegion);
26+
27+
// 显示原始图片和裁剪后的图片
28+
cv::imshow("原始图片", image);
29+
cv::imshow("裁剪后的图片", croppedImage);
30+
cv::waitKey(0);
31+
32+
// 保存裁剪后的图片
33+
cv::imwrite("../cropped_lena.jpg", croppedImage);
34+
35+
std::cout << "Hello, World!" << std::endl;
36+
return 0;
37+
}

0 commit comments

Comments
 (0)