在彩色图像上画一条直线:
for item in lc_imglists:
img_ori = cv2.imread(item)
cv2.line(img_ori,(100,100),(1000,1000),(0,0,255),3)
imshow(img_ori,600,600,'img',True)
如果对原图像上下,或左右翻转后再画直线,第一想法是:
for item in lc_imglists:
img = cv2.imread(item)[:,0:2000,:]
img = np.flipud(img) # np.fliplr
cv2.line(img,(100,100),(1000,1000),(0,0,255),3)
imshow(img,600,600,'img',True)
图像中没有显示所画的红线,退而求其次,对每个通道进行翻转,再进行拼接:
for item in lc_imglists:
img = cv2.imread(item)[:,0:2000,:]
flag = True
if flag:
b,g,r = cv2.split(img)
b = np.fliplr(b)
g = np.fliplr(g)
r = np.fliplr(r)
img[:,:,0] = b
img[:,:,1] = g
img[:,:,2] = r
cv2.line(img,(100,100),(1000,1000),(0,0,255),3)
imshow(img,600,600,'img',True)
水平 竖直投影法在图像分割中应用广泛, 代码实现如下:
def CalcImgProjection(img):
rowProjection = np.mean(img, axis=0)
colProjection = np.mean(img, axis=1)
plt.figure()
plt.subplot(121)
plt.imshow(img, 'gray')
plt.title('img')
plt.subplot(222)
plt.plot(rowProjection, 'r*')
plt.title('row')
plt.subplot(224)
plt.plot(colProjection, 'go')
plt.title('col')
plt.show()
if __name__ == '__main__':
data = './datasets/lowcontrast_thread/'
type = '*.bmp'
imglists = glob.glob(data + type)
for item in imglists:
img = cv2.imread(item,0)[:,:2500]
CalcImgProjection(img)
利用像素值的变化关系,选择合理的阈值,即可实现目标区域的分割.