[opencv]6.图片的处理:侵蚀、膨胀、反转

本文介绍了如何使用OpenCV库对图片进行侵蚀、膨胀和反转处理。在实现过程中,需要将图片复制到工程目录,并处理编译警告,如禁用fopen的安全警告。最终成功运行后,图片将按预期进行处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文的的目标就是实现对图片的侵蚀、膨胀和反转。
具体的代码如下:

#include "stdafx.h"//由于是新建的空工程,所以需要新建stdafx.h和targetver.h两个头文件,不然会出现找不到头文件的问题
#include "opencv/cv.h"
#include "opencv/highgui.h"

int main()
{
    //display the original image
    IplImage* img = cvLoadImage("pic.jpg");
    IplImage* imgErode = cvLoadImage("pic.jpg");
    IplImage* imgDilate = cvLoadImage("pic.jpg");
    IplImage* imgInvert = cvLoadImage("pic.jpg");
    cvNamedWindow("MyWindow");
    cvShowImage("MyWindow", img);

    // 侵蚀
    cvErode(img, imgErode, 0, 10);
    cvNamedWindow("Eroded");
    cvShowImage("Eroded", imgErode);
    //膨胀 
    cvDilate(img, imgDilate, 0, 3);
    cvNamedWindow("Dilated");
    cvShowImage("Dilated", imgDilate);
    // 反转
    cvNot(img, imgInvert);
    cvNamedWindow("Inverted");
    cvShowImage("Inverted", imgInvert);

    cvWaitKey(0);

    //cleaning up
    cvDestroyWindow("MyWindow");
    cvDestroyWindow("Eroded");
    cvReleaseImage(&img);

    return 0;
}

1.需要把pic.jpg拷贝到工程目录同cpp文件的文件夹下。
2.需要定义stdafx.h和targetver.h头文件。

//stdafx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

//targetver.h
#pragma once

// Including SDKDDKVer.h defines the highest available Windows platform.

// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.

#include <SDKDDKVer.h>

3.直接运行会碰到fopen方法不安全(error:C4996)的问题。
问题提示见下图:
这里写图片描述
解决办法:加上#pragma warning(disble:4996)这句话
这里写图片描述
4.运行效果如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值