图像理解大作业(二):调用Python编程实现Sobel算子,Laplacian算子和区域生长法。

  • 问题描述

调用Python编程实现Sobel算子,Laplacian算子和区域生长法。

 

  • 实验结果

1 原图像

2 Sobel算子

3 Laplacian算子

4 区域生长法

  • 代码展示

1 Sobel算子

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('1.jpg')
d = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
sp = d.shape
print(sp)
height = sp[0]
weight = sp[1]
sx = np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
sy = np.array([[-1,-2,-1],[0,0,0],[1,2,1]])
dSobel = np.zeros((height,weight))
dSobelx = np.zeros((height,weight))
dSobely = np.zeros((height,weight))
Gx = np.zeros(d.shape)
Gy = np.zeros(d.shape)
for i in range(height-2):
    for j in range(weight-2):
        Gx[i + 1, j + 1] = abs(np.sum(d[i:i + 3, j:j + 3] * sx))
        Gy[i + 1, j + 1] = abs(np.sum(d[i:i + 3, j:j + 3] * sy))
        dSobel[i+1, j+1] = (Gx[i+1, j+1]*Gx[i+1,j+1] + Gy[i+1, j+1]*Gy[i+1,j+1])**0.5
        dSobelx[i+1, j+1] = np.sqrt(Gx[i+1, j+1])
        dSobely[i + 1, j + 1] = np.sqrt(Gy[i + 1, j + 1])

cv2.imshow('a', img)
cv2.imshow('b', d)
cv2.imshow('c', np.uint8(dSobel))
cv2.imshow('d', np.uint8(dSobelx))
cv2.imshow('e', np.uint8(dSobely))
cv2.waitKey(0)
cv2.destroyAllWindows()

a = np.uint8(dSobel)
b = np.uint8(dSobelx)
c = np.uint8(dSobel)
img = img[:, :, ::-1]

plt.subplot(321), plt.imshow(img), plt.title('f')
plt.subplot(322), plt.imshow(d), plt.title('g')
plt.subplot(323), plt.imshow(a), plt.title('w')
plt.subplot(324), plt.imshow(b), plt.title('q')
plt.subplot(325), plt.imshow(c), plt.title('e')
plt.show()

2 Laplacian算子

# coding=utf-8
import cv2
import numpy as np

img = cv2.imread("1.jpg", 0)

gray_lap = cv2.Laplacian(img, cv2.CV_16S, ksize=3)
dst = cv2.convertScaleAbs(gray_lap)

cv2.imshow('laplacian', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()

3 区域生长法

import numpy as np
import cv2
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def getX(self):
        return self.x

    def getY(self):
        return self.y


def getGrayDiff(img, currentPoint, tmpPoint):
    return abs(int(img[currentPoint.x, currentPoint.y]) - int(img[tmpPoint.x, tmpPoint.y]))


def selectConnects(p):
    if p != 0:
        connects = [Point(-1, -1), Point(0, -1), Point(1, -1), Point(1, 0), Point(1, 1), \
                    Point(0, 1), Point(-1, 1), Point(-1, 0)]
    else:
        connects = [Point(0, -1), Point(1, 0), Point(0, 1), Point(-1, 0)]
    return connects


def regionGrow(img, seeds, thresh, p=1):
    height, weight = img.shape
    seedMark = np.zeros(img.shape)
    seedList = []
    for seed in seeds:
        seedList.append(seed)
    label = 1
    connects = selectConnects(p)
    while (len(seedList) > 0):
        currentPoint = seedList.pop(0)

        seedMark[currentPoint.x, currentPoint.y] = label
        for i in range(8):
            tmpX = currentPoint.x + connects[i].x
            tmpY = currentPoint.y + connects[i].y
            if tmpX < 0 or tmpY < 0 or tmpX >= height or tmpY >= weight:
                continue
            grayDiff = getGrayDiff(img, currentPoint, Point(tmpX, tmpY))
            if grayDiff < thresh and seedMark[tmpX, tmpY] == 0:
                seedMark[tmpX, tmpY] = label
                seedList.append(Point(tmpX, tmpY))
    return seedMark


img = cv2.imread('1.jpg', 0)
seeds = [Point(10, 10), Point(82, 150), Point(20, 300)]
binaryImg = regionGrow(img, seeds, 10)
cv2.imshow(' ', binaryImg)
cv2.waitKey(0)
cv2.imwrite('3_.jpg', binaryImg)

 

猜你喜欢

转载自blog.csdn.net/shunzi1013238638/article/details/107494630
今日推荐