用c语言改写import cv2 import numpy as np #读取原始图像 img = cv2.imread('scenery.png', 1) #获取图像的高度和宽度 height, width = img.shape[:2] #图像灰度处理 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #创建目标图像 dstImg = np.zeros((height,width,1),np.uint8) #浮雕特效算法:newPixel = grayCurrentPixel - grayNextPixel + 150 for i in range(0,height): for j in range(0,width-1): grayCurrentPixel = int(gray[i,j]) grayNextPixel = int(gray[i,j+1]) newPixel = grayCurrentPixel - grayNextPixel + 150 if newPixel > 255: newPixel = 255 if newPixel < 0: newPixel = 0 dstImg[i,j] = newPixel #显示图像 cv2.imshow('src', img) cv2.imshow('dst',dstImg) #等待显示 cv2.waitKey() cv2.destroyAllWindows()
时间: 2023-12-05 07:04:25 浏览: 78
以下是使用C语言改写后的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
//读取原始图像
Mat img = imread("scenery.png", IMREAD_COLOR);
if (img.empty())
{
printf("Could not read the image: %s\n", argv[1]);
return -1;
}
//获取图像的高度和宽度
int height = img.rows;
int width = img.cols;
//图像灰度处理
Mat gray;
cvtColor(img, gray, COLOR_BGR2GRAY);
//创建目标图像
Mat dstImg(height, width, CV_8UC1);
//浮雕特效算法:newPixel = grayCurrentPixel - grayNextPixel + 150
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width - 1; j++)
{
int grayCurrentPixel = (int)gray.at<uchar>(i, j);
int grayNextPixel = (int)gray.at<uchar>(i, j + 1);
int newPixel = grayCurrentPixel - grayNextPixel + 150;
if (newPixel > 255)
{
newPixel = 255;
}
if (newPixel < 0)
{
newPixel = 0;
}
dstImg.at<uchar>(i, j) = (uchar)newPixel;
}
}
//显示图像
namedWindow("src", WINDOW_AUTOSIZE);
namedWindow("dst", WINDOW_AUTOSIZE);
imshow("src", img);
imshow("dst", dstImg);
//等待显示
waitKey(0);
destroyAllWindows();
return 0;
}
```
注意:在使用该代码之前,需要先安装OpenCV库,并确保编译器能够正确链接该库。
阅读全文