练习1 图像处理基本绘制操作(附matlab+python代码)

这篇博客介绍了如何使用Python和Matlab进行图像处理,包括将彩色图像转换为灰度图,然后在灰度图上绘制黑色方块、黑白棋盘格和白色三角形。此外,还展示了在彩色图上绘制红色圆盘和白色椭圆轮廓的方法。通过这些示例,读者可以学习到基本的图像操作技巧。
摘要由CSDN通过智能技术生成

题目:
1.读取灰度图
2. 灰度图:
绘制黑色方块、黑白棋盘格、白色三角块
在这里插入图片描述

3.彩色图:
绘制红色圆盘、白色椭圆轮廓
在这里插入图片描述

代码:

灰度图:

#python
import cv2
img = cv2.imread('lena_512.jpg')
gray = img[:,:,0]
blue = img[:,:,0]
green = img[:,:,1]
red = img[:,:,2]
[h,w]=img.shape[:2]
for i in range(h):
   for j in range(w):
       gray[i,j]=blue[i,j]*0.114+green[i,j]*0.587+red[i,j]*0.299
cv2.imshow('image',gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
% matlab
img =imread('cat.jpg');
gray=img(:,:,1);
blue=img(:,:,1);
green=img(:,:,2);
red=img(:,:,3);
[h,w]=size(img);
for i=1:h
    
    for j=1:w/3
        gray(i,j)=blue(i,j)*0.114+green(i,j)*0.587+red(i,j)*0.299;
        
    end
end
imshow(gray)

画方块

#python
import cv2
img = cv2.imread('lena_512.jpg')
h,w = img.shape[:2]
x = h//3
y = w//3
for i in range(x,2*x):
    for j in range(y,2*y):
        img[i,j,:]=255
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
% matlab
[a,b]=size(gray);
x=floor(a/3);
y=floor(b/3);
for i=x:2*x
    for j=y:2*y
        gray(i,j)=255;
    end
end
imshow(gray);

画红圆

#python
import cv2
img = cv2.imread('lena_512.jpg')
h,w = img.shape[:2]
x = h//3
y = w//3
for i in range(h):
    for j in range(w):
        if (pow((x-i)*(x-i)+(y-j)*(y-j),0.5)<50):
            img[i,j,0] = 0
            img[i,j,1] = 0
            img[i,j,2] = 255

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

%matlab
img=imread('cat.jpg');
[h,w]=size(img);
x = floor(h/2);
y = floor(w/6);
for i=1:h
    for j=1:w/3
        if (x-i)*(x-i)+(y-j)*(y-j)<2500
            img(i,j,1) = 255;
            img(i,j,2) = 0;
            img(i,j,3) = 0;
        end
    end
end
imshow(img)

画三角块
python

matlab
画黑白棋盘格

pythonmatlab
画椭圆
python
matlab

可以使用OpenCV库来实现对图像中轮廓最小包围圆圈的截取。具体步骤如下: 1. 使用OpenCV库读入图像,将其转换为灰度图像,并进行二值化处理。 ```python import cv2 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) ``` 2. 使用OpenCV库的findContours函数找到图像中的轮廓,并使用drawContours函数将其绘制在原图上。 ```python contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(img, contours, -1, (0, 0, 255), 2) ``` 3. 对于每个轮廓,使用minEnclosingCircle函数找到最小包围圆,并使用circle函数在原图上绘制出该圆。 ```python for contour in contours: (x, y), radius = cv2.minEnclosingCircle(contour) center = (int(x), int(y)) radius = int(radius) cv2.circle(img, center, radius, (0, 255, 0), 2) ``` 4. 对于每个最小包围圆,使用切片操作在原图上截取出其所包含的部分。 ```python for i, contour in enumerate(contours): (x, y), radius = cv2.minEnclosingCircle(contour) x, y, radius = int(x), int(y), int(radius) crop = img[y - radius:y + radius, x - radius:x + radius] cv2.imwrite('crop_{}.jpg'.format(i), crop) ``` 完整代码如下: ```python import cv2 img = cv2.imread('image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ret, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) contours, hierarchy = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for i, contour in enumerate(contours): (x, y), radius = cv2.minEnclosingCircle(contour) x, y, radius = int(x), int(y), int(radius) crop = img[y - radius:y + radius, x - radius:x + radius] cv2.imwrite('crop_{}.jpg'.format(i), crop) cv2.imshow('img', img) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值