Python代码实现OpenCV初始化黑色底图像
时间: 2023-02-08 22:50:46 浏览: 132
可以使用OpenCV在Python中创建黑色底图像。下面是一个示例代码:
```python
import cv2
# 创建大小为(800, 600),颜色为黑色的图像
img = cv2.imread("black.jpg",cv2.IMREAD_GRAYSCALE)
#或
img = np.zeros((800, 600, 3), np.uint8)
# 显示图像
cv2.imshow("Black Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
第一行引用了OpenCV的Python模块,第二行创建了大小为(800, 600),颜色为黑色(颜色值为0)的图像。
第三行是显示图像,最后使用cv2.destroyAllWindows()函数关闭窗口
相关问题
自适应二值化python代码实现 不调用opencv
自适应二值化是指根据图像的局部特征来确定阈值的二值化方法。以下是一个简单的 Python 实现:
```python
import numpy as np
def adaptive_threshold(img, block_size, constant):
# 块的大小必须为奇数
if block_size % 2 == 0:
block_size += 1
# 图像的大小
h, w = img.shape
# 初始化二值化后的图像
binary_img = np.zeros((h, w), np.uint8)
# 遍历图像的每个像素
for y in range(h):
for x in range(w):
# 计算像素所在块的左上角和右下角坐标
x1 = max(x - block_size // 2, 0)
y1 = max(y - block_size // 2, 0)
x2 = min(x + block_size // 2, w - 1)
y2 = min(y + block_size // 2, h - 1)
# 计算块内的平均灰度值
block = img[y1:y2+1, x1:x2+1]
mean = np.mean(block)
# 根据块内的平均灰度值计算阈值
threshold = mean - constant
# 根据阈值将像素二值化
if img[y, x] > threshold:
binary_img[y, x] = 255
else:
binary_img[y, x] = 0
return binary_img
```
使用示例:
```python
import cv2
img = cv2.imread('test.jpg', cv2.IMREAD_GRAYSCALE)
binary_img = adaptive_threshold(img, block_size=41, constant=15)
cv2.imshow('binary', binary_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
其中,`block_size` 参数表示块的大小,`constant` 参数表示阈值偏移量。
霍夫圆检测python代码实现,不调用opencv
以下是一个基于霍夫变换的圆检测的 Python 代码实现,不使用 OpenCV:
```python
import numpy as np
import matplotlib.pyplot as plt
def hough_transform(img, rmin, rmax):
# 将图像转换为灰度图像
gray = img.copy()
if len(img.shape) == 3:
gray = np.mean(img, axis=2)
# 定义霍夫变换的参数
rows, cols = gray.shape
theta = np.arange(0, 360, 1)
radii = np.arange(rmin, rmax+1, 1)
# 计算 sin 和 cos 值,以加速计算过程
sin_theta = np.sin(np.deg2rad(theta))
cos_theta = np.cos(np.deg2rad(theta))
# 初始化空的霍夫累加矩阵
hough_acc = np.zeros((len(radii), len(theta)))
# 遍历所有像素,查找可能的圆心
for r_idx, r in enumerate(radii):
for y in range(rows):
for x in range(cols):
if gray[y, x] > 0:
# 对于每个可能的圆心,计算半径
for t_idx, _ in enumerate(theta):
a = int(x - r * cos_theta[t_idx])
b = int(y - r * sin_theta[t_idx])
if a >= 0 and b >= 0 and a < cols and b < rows:
hough_acc[r_idx, t_idx] += 1
return hough_acc, radii, theta
def find_circles(img, num_circles, rmin, rmax):
# 使用霍夫变换查找圆形
hough_acc, radii, theta = hough_transform(img, rmin, rmax)
# 找到最可能的圆形
best_circles = []
for i in range(num_circles):
r_idx, t_idx = np.unravel_index(np.argmax(hough_acc), hough_acc.shape)
r = radii[r_idx]
t = theta[t_idx]
best_circles.append((r, t))
# 将该圆形从累加器中删除,以便找到下一个最可能的圆形
for y in range(img.shape[0]):
for x in range(img.shape[1]):
if img[y, x] > 0:
a = int(x - r * np.cos(np.deg2rad(t)))
b = int(y - r * np.sin(np.deg2rad(t)))
if a >= 0 and b >= 0 and a < img.shape[1] and b < img.shape[0]:
hough_acc[r_idx, t_idx] -= 1
return best_circles
# 测试代码
img = np.zeros((100, 100))
img[50, 50] = 1
img[40, 60] = 1
img[60, 60] = 1
circles = find_circles(img, 1, 10, 30)
for r, t in circles:
a = int(50 - r * np.cos(np.deg2rad(t)))
b = int(50 - r * np.sin(np.deg2rad(t)))
img[a-2:a+2, b-2:b+2] = 1
plt.imshow(img)
plt.show()
```
这个实现基于霍夫变换来检测圆形,它计算每个像素对于所有可能的圆心和半径的贡献,并在累加器中计算每个组合的得分。最后,它找到最可能的圆形并从累加器中删除,以便找到下一个最可能的圆形。