Skip to content

Commit

Permalink
feat(samples): add OpenCVDemo
Browse files Browse the repository at this point in the history
  • Loading branch information
zjykzj committed May 5, 2024
1 parent d2c2c84 commit 2bf535f
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
66 changes: 66 additions & 0 deletions samples/OpenCVDemo/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated from CLion C/C++ Code Style settings
BasedOnStyle: LLVM
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: None
AlignOperands: DontAlign
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Always
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 0
CompactNamespaces: false
ContinuationIndentWidth: 4
IndentCaseLabels: true
IndentPPDirectives: None
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PointerAlignment: Right
ReflowComments: false
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 0
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never
1 change: 1 addition & 0 deletions samples/OpenCVDemo/3rdparty/opencv
21 changes: 21 additions & 0 deletions samples/OpenCVDemo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.24)
project(OpenCVDemo)

set(CMAKE_CXX_STANDARD 17)

# set opencv
get_filename_component(ABSOLUTE_OpenCV_DIR ./3rdparty/opencv ABSOLUTE)
IF (CMAKE_SYSTEM_NAME MATCHES "Linux")
set(OpenCV_DIR ${ABSOLUTE_OpenCV_DIR}/lib/cmake/opencv4)
ELSEIF (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(OpenCV_DIR ${ABSOLUTE_OpenCV_DIR})
ENDIF ()
find_package(OpenCV REQUIRED)

MESSAGE("OpenCV version: ${OpenCV_VERSION}")
MESSAGE("OpenCV OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
MESSAGE("OpenCV OpenCV_LIBS: ${OpenCV_LIBS}")


add_executable(OpenCVDemo main.cpp)
target_link_libraries(OpenCVDemo ${OpenCV_LIBS})
Binary file added samples/OpenCVDemo/cropped_lena.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/OpenCVDemo/lena.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions samples/OpenCVDemo/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>

#include "opencv2/opencv.hpp"

int main() {
// 读取图片
cv::Mat image = cv::imread("../lena.jpg");
if (image.empty()) {
std::cout << "无法读取图片,请确保文件路径正确" << std::endl;
return -1;
}

// 获取图片的尺寸
int width = image.cols;
int height = image.rows;

// 计算中心裁剪的区域
int x = width / 4; // 裁剪区域的左上角 x 坐标
int y = height / 4; // 裁剪区域的左上角 y 坐标
int cropWidth = width / 2; // 裁剪区域的宽度
int cropHeight = height / 2;// 裁剪区域的高度

// 裁剪图片
cv::Rect cropRegion(x, y, cropWidth, cropHeight);
cv::Mat croppedImage = image(cropRegion);

// 显示原始图片和裁剪后的图片
cv::imshow("原始图片", image);
cv::imshow("裁剪后的图片", croppedImage);
cv::waitKey(0);

// 保存裁剪后的图片
cv::imwrite("../cropped_lena.jpg", croppedImage);

std::cout << "Hello, World!" << std::endl;
return 0;
}

0 comments on commit 2bf535f

Please sign in to comment.