Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用OpenCV和Python创建语义映射_Python_Opencv_Edge Detection - Fatal编程技术网

如何使用OpenCV和Python创建语义映射

如何使用OpenCV和Python创建语义映射,python,opencv,edge-detection,Python,Opencv,Edge Detection,我正在做一个学校项目,我需要从相机图片中创建一个语义图。这幅画是一块农田。我们的目标是在没有CNN或学习方法的帮助下创建它,而仅仅是经典的检测方法 我搜索了类似的方法和算法,这是我的代码和结果,但该算法不能很好地检测图像中各个区域的边缘 from __future__ import print_function import cv2 as cv import numpy as np import imutils import argparse import random as rng rng.s

我正在做一个学校项目,我需要从相机图片中创建一个语义图。这幅画是一块农田。我们的目标是在没有CNN或学习方法的帮助下创建它,而仅仅是经典的检测方法

我搜索了类似的方法和算法,这是我的代码和结果,但该算法不能很好地检测图像中各个区域的边缘

from __future__ import print_function
import cv2 as cv
import numpy as np
import imutils
import argparse
import random as rng
rng.seed(12345)

src = cv.imread('field.jpg')
src = imutils.resize(src, width=600)

# Show source image
cv.imshow('Source Image', src)
src[np.all(src == 255, axis=2)] = 0
# Show output image
cv.imshow('Black Background Image', src)
kernel = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]], dtype=np.float32)
# do the laplacian filtering as it is
# well, we need to convert everything in something more deeper then CV_8U
# because the kernel has some negative values,
# and we can expect in general to have a Laplacian image with negative values
# BUT a 8bits unsigned int (the one we are working with) can contain values from 0 to 255
# so the possible negative number will be truncated
imgLaplacian = cv.filter2D(src, cv.CV_32F, kernel)
sharp = np.float32(src)
imgResult = sharp - imgLaplacian
# convert back to 8bits gray scale
imgResult = np.clip(imgResult, 0, 255)
imgResult = imgResult.astype('uint8')
imgLaplacian = np.clip(imgLaplacian, 0, 255)
imgLaplacian = np.uint8(imgLaplacian)
#cv.imshow('Laplace Filtered Image', imgLaplacian)
cv.imshow('New Sharped Image', imgResult)
bw = cv.cvtColor(imgResult, cv.COLOR_BGR2GRAY)
_, bw = cv.threshold(bw, 125, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow('Binary Image', bw)
dist = cv.distanceTransform(bw, cv.DIST_L2, 3)
# Normalize the distance image for range = {0.0, 1.0}
# so we can visualize and threshold it
cv.normalize(dist, dist, 0, 1.0, cv.NORM_MINMAX)
cv.imshow('Distance Transform Image', dist)
_, dist = cv.threshold(dist, 0.4, 1.0, cv.THRESH_BINARY)
# Dilate a bit the dist image
kernel1 = np.ones((3,3), dtype=np.uint8)
dist = cv.dilate(dist, kernel1)
cv.imshow('Peaks', dist)
dist_8u = dist.astype('uint8')
# Find total markers
_, contours, _ = cv.findContours(dist_8u, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Create the marker image for the watershed algorithm
markers = np.zeros(dist.shape, dtype=np.int32)
# Draw the foreground markers
for i in range(len(contours)):
    cv.drawContours(markers, contours, i, (i+1), -1)
# Draw the background marker
cv.circle(markers, (5,5), 3, (255,255,255), -1)
cv.imshow('Markers', markers*10000)
cv.watershed(imgResult, markers)
#mark = np.zeros(markers.shape, dtype=np.uint8)
mark = markers.astype('uint8')
mark = cv.bitwise_not(mark)
# uncomment this if you want to see how the mark
# image looks like at that point
#cv.imshow('Markers_v2', mark)
# Generate random colors
colors = []
for contour in contours:
    colors.append((rng.randint(0,256), rng.randint(0,256), rng.randint(0,256)))
# Create the result image
dst = np.zeros((markers.shape[0], markers.shape[1], 3), dtype=np.uint8)
# Fill labeled objects with random colors
for i in range(markers.shape[0]):
    for j in range(markers.shape[1]):
        index = markers[i,j]
        if index > 0 and index <= len(contours):
            dst[i,j,:] = colors[index-1]
# Visualize the final image
cv.imshow('Final Result', dst)
cv.waitKey()
``



  [1]: https://i.stack.imgur.com/qpE4s.jpg
from\uuuuu future\uuuuu导入打印功能
将cv2作为cv导入
将numpy作为np导入
导入imutils
导入argparse
将随机导入为rng
种子(12345)
src=cv.imread('field.jpg')
src=imutils.resize(src,width=600)
#显示源图像
cv.imshow('源图像',src)
src[np.all(src==255,轴=2)]=0
#显示输出图像
cv.imshow('黑色背景图像',src)
kernel=np.array([[1,1,1],[1,-8,1],[1,1,1]],dtype=np.float32)
#按原样进行拉普拉斯滤波
#好吧,我们需要把一切转换成比CV_8U更深刻的东西
#因为内核有一些负值,
#一般来说,我们可以期望有一个带负值的拉普拉斯图像
#但是8位无符号整数(我们正在使用的整数)可以包含0到255之间的值
#因此,可能的负数将被截断
imgLaplacian=cv.filter2D(src,cv.cv_32F,内核)
夏普=np.32(src)
imgResult=夏普-imgLaplacian
#转换回8比特的灰度
imgResult=np.clip(imgResult,0255)
imgResult=imgResult.astype('uint8')
imgLaplacian=np.clip(imgLaplacian,0255)
imgLaplacian=np.uint8(imgLaplacian)
#cv.imshow(“拉普拉斯过滤图像”,imgLaplacian)
cv.imshow(“新锐化图像”,imgResult)
bw=cv.CVT颜色(imgResult,cv.COLOR\u bgr2灰色)
_,bw=cv.阈值(bw,125,255,cv.THRESH_BINARY | cv.THRESH_OTSU)
cv.imshow('二进制图像',bw)
距离=cv.distanceTransform(bw,cv.DistanceL2,3)
#规范化范围={0.0,1.0}的距离图像
#这样我们就可以把它形象化并设定阈值
cv.normalize(距离,距离,0,1.0,cv.Normal\u最小值)
cv.imshow(“距离变换图像”,距离)
_,dist=cv.阈值(dist,0.4,1.0,cv.THRESH_二进制)
#把远处的影像放大一点
内核1=np.ones((3,3),dtype=np.uint8)
距离=cv.扩张(距离,核1)
cv.imshow(“峰值”,距离)
dist_8u=dist.astype('uint8')
#查找总标记
_,等高线,等高线=等高线(等高线8u,等高线外部,等高线链近似简单)
#为分水岭算法创建标记图像
markers=np.zero(dist.shape,dtype=np.int32)
#绘制前景标记
对于范围内的i(透镜(轮廓)):
等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线等高线
#绘制背景标记
cv.圆圈(标记,(5,5),3,(255255),-1)
cv.imshow('Markers',Markers*10000)
cv.流域(imgResult,标记)
#mark=np.zero(markers.shape,dtype=np.uint8)
mark=markers.astype('uint8')
标记=cv.按位\u非(标记)
#如果要查看标记是如何显示的,请取消对此的注释
#图像在那个点上看起来像
#cv.imshow('Markers_v2',mark)
#生成随机颜色
颜色=[]
对于等高线中的等高线:
colors.append((rng.randint(0256)、rng.randint(0256)、rng.randint(0256)))
#创建结果图像
dst=np.zero((markers.shape[0],markers.shape[1],3),dtype=np.uint8)
#用随机颜色填充标记的对象
对于范围内的i(markers.shape[0]):
对于范围内的j(标记形状[1]):
索引=标记[i,j]

如果索引>0且索引为0,您是否也可以像共享结果一样上载'field.jpg'图像?@B.Kocis在上一条评论中我向您发送了图片链接您还可以像共享结果一样上载'field.jpg'图像?@B.Kocis在上一条评论中我向您发送了图片链接