OpenCV通过指针裁剪图像

OpenCV 中mat 格式的像素数值都是连续排列的。为了深入了解cuda 编程。我们来写一个简单的小程序测试一下。

1 不裁剪

cpp 复制代码
 cv::Mat crop_image(int(height), int(width), CV_8UC3, image.data);

2 只保留图像1/3

cpp 复制代码
cv::Mat crop_image(int(height/3), int(width), CV_8UC3, image.data);

3 每行错开一个像素

cpp 复制代码
cv::Mat crop_image(int(height), int(width-1), CV_8UC3, image.data);

.

4 .全部代码

cpp 复制代码
#include <opencv2/opencv.hpp> // 包含OpenCV的主要头文件
#include <iostream>
#include <direct.h> 
int main() 
{
    cv::Mat image; // 创建一个Mat对象来存储图像
    char runPath[1024] = { 0 };
    image = cv::imread("mountain.png", cv::IMREAD_COLOR); // 读取图片
    if (image.empty()) 
    { // 检查图片是否成功加载
        std::cout << "Could not open or find the image\n";
        return -1;
    }
    cv::namedWindow("src_image", cv::WINDOW_AUTOSIZE); // 创建一个窗口
    std::cout <<"image size: "<< image.size() << std::endl;
    int width = image.cols;
    int height = image.rows;
    std::cout << "x: " << image.cols << std::endl;
    std::cout <<"y: "<< image.rows << std::endl;
    cv::Mat crop_image(int(height/3), int(width), CV_8UC3, image.data);
    cv::imshow("src_image", image); // 在窗口中显示图片
    cv::waitKey(0); // 等待按键事件,0表示无限期等待   
    cv::imshow("crop", crop_image); // 在窗口中显示图片
    cv::waitKey(0); // 等待按键事件,0表示无限期等待   
       
    _getcwd(runPath, sizeof(runPath));
    std::cout<<"running path" << runPath << std::endl;

    return 0;
}
相关推荐
微学AI1 分钟前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
城管不管5 分钟前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
boppu23 分钟前
布草特殊污渍去渍剂的种类及作用
大数据·人工智能
AIsoft_868840 分钟前
会议录音转文字与AI纪要工具推荐:免费额度与核心功能对比指南
人工智能
sphw41 分钟前
nixnb: Jupyter Notebook 优雅分享
ide·人工智能·jupyter
mingo_敏1 小时前
DeepAgents : 权限(Permissions)
人工智能·深度学习·langchain
合调于形1 小时前
Bianfchheng (Liuu) 《边城(六)》字母标调拼音拼写实测案例
人工智能·自然语言处理·人机交互·语音识别·学习方法
凯丨1 小时前
AI 蠕虫来了:恶意文档如何通过 Copilot for Word 自我传播?
人工智能·c#·copilot
武子康1 小时前
GPT-5.6 Luna 降价 80%:Agent 真正该重算的是单次成功成本
人工智能·chatgpt·llm
Gu Gu Study1 小时前
ScoutLoop开放域深度研究引擎(agent的初步设计想法)
人工智能·python