我正在开发将使用分水岭的图像分割应用程序。为此,我找到了需要在python中使用的代码。但是,我很难转换为Kotlin,因为Mat
Mat()不具有zero_likes函数,只有0函数。我正在使用opencv 3.31。我该如何在Kotlin中进行检查:
marked[marked == 1] = 0
marked[marked > 1] = 255
程式码python:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Load the image
img = cv2.imread("/path/to/image.png", 3)
# Create a blank image of zeros (same dimension as img)
# It should be grayscale (1 color channel)
marker = np.zeros_like(img[:,:,0]).astype(np.int32)
# This step is manual. The goal is to find the points
# which create the result we want. I suggest using a
# tool to get the pixel coordinates.
# Dictate the background and set the markers to 1
marker[204][95] = 1
marker[240][137] = 1
marker[245][444] = 1
marker[260][427] = 1
marker[257][378] = 1
marker[217][466] = 1
# Dictate the area of interest
# I used different values for each part of the car (for visibility)
marker[235][370] = 255 # car body
marker[135][294] = 64 # rooftop
marker[190][454] = 64 # rear light
marker[167][458] = 64 # rear wing
marker[205][103] = 128 # front bumper
# rear bumper
marker[225][456] = 128
marker[224][461] = 128
marker[216][461] = 128
# front wheel
marker[225][189] = 192
marker[240][147] = 192
# rear wheel
marker[258][409] = 192
marker[257][391] = 192
marker[254][421] = 192
# Now we have set the markers, we use the watershed
# algorithm to generate a marked image
marked = cv2.watershed(img, marker)
# Plot this one. If it does what we want, proceed;
# otherwise edit your markers and repeat
plt.imshow(marked, cmap='gray')
plt.show()
# Make the background black, and what we want to keep white
marked[marked == 1] = 0
marked[marked > 1] = 255
# Use a kernel to dilate the image, to not lose any detail on the outline
# I used a kernel of 3x3 pixels
kernel = np.ones((3,3),np.uint8)
dilation = cv2.dilate(marked.astype(np.float32), kernel, iterations = 1)
# Plot again to check whether the dilation is according to our needs
# If not, repeat by using a smaller/bigger kernel, or more/less iterations
plt.imshow(dilation, cmap='gray')
plt.show()
# Now apply the mask we created on the initial image
final_img = cv2.bitwise_and(img, img, mask=dilation.astype(np.uint8))
# cv2.imread reads the image as BGR, but matplotlib uses RGB
# BGR to RGB so we can plot the image with accurate colors
b, g, r = cv2.split(final_img)
final_img = cv2.merge([r, g, b])
# Plot the final result
plt.imshow(final_img)
plt.show()
代码kotlin:
// Load the image
val srcOriginal = Imgcodecs.imread(currentPhotoPath)
// Create a blank image of zeros (same dimension as img)
// It should be grayscale (1 color channel)
val markers = Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32S)
// This step is manual. The goal is to find the points
// which create the result we want. I suggest using a
// tool to get the pixel coordinates.
// Dictate the area of interest
for(x in my_canvas.pointsToDrawX.indices) {
for(y in my_canvas.pointsToDrawY.indices) {
markers.put(
my_canvas.pointsToDrawX.get(x).toInt(),
my_canvas.pointsToDrawY.get(y).toInt(),
255.0
)
}
}
//Now we have set the markers, we use the watershed
//algorithm to generate a marked image
Imgproc.watershed(srcOriginal, markers)
val marker_tempo = Mat()
markers.convertTo(marker_tempo, CvType.CV_8U)
// Plot this one. If it does what we want, proceed;
// otherwise edit your markers and repeat
//Create Bitmap
val bmpOut = Bitmap.createBitmap(srcOriginal.cols(), srcOriginal.rows(), Bitmap.Config.RGB_565)
Utils.matToBitmap(marker_tempo, bmpOut)
val mPath = Environment.getExternalStorageDirectory().toString() + "/gray.png"
Imgcodecs.imwrite(mPath,marker_tempo)
//Make the background black, and what we want to keep white
//Use a kernel to dilate the image, to not lose any detail on the outline
//I used a kernel of 3x3 pixels
val kernel = Mat(3, 3, CvType.CV_8U)
val dilatation = Imgproc.dilate(marker_tempo, marker_tempo, kernel)
val mPath1 = Environment.getExternalStorageDirectory().toString() + "/dilation.png"
Imgcodecs.imwrite(mPath1,marker_tempo)
//Now apply the mask we created on the initial image
val final_image = Core.bitwise_and(srcOriginal, srcOriginal, dilatation)
//cv2.imread reads the image as BGR, but matplotlib uses RGB
//BGR to RGB so we can plot the image with accurate colors
在pointsToDrawX和pointsToDrawY中,我将用户触摸事件的所有x,y坐标保存在屏幕上。正是从这些坐标中,我将传递到分水岭算法来执行分割并从图像中删除背景。有人可以帮我转换此代码吗?
//Load the image
srcOriginal = Imgcodecs.imread(currentPhotoPath)
//Create a blank image of zeros (same dimension as img)
//It should be grayscale (1 color channel)
markers = Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32S)
//This step is manual. The goal is to find the points
//which create the result we want. I suggest using a
//tool to get the pixel coordinates.
//Dictate the background and set the markers to 1
for (value in 0..my_canvas.pointsToDrawY.size - 1) {
markers.put(
my_canvas.pointsToDrawX[value].toInt(),
my_canvas.pointsToDrawY[value].toInt(),
1.0
)
}
//Dictate the area of interest
//I used different values for each part of the car (for visibility)
for (value in 0..my_canvas.pointsToDrawYStepTwo.size - 1) {
markers.put(
my_canvas.pointsToDrawXStepTwo[value].toInt(),
my_canvas.pointsToDrawYStepTwo[value].toInt(),
255.0
)
}
//Now we have set the markers, we use the watershed
//algorithm to generate a marked image
watershed(srcOriginal, markers)
//Plot this one. If it does what we want, proceed;
//otherwise edit your markers and repeat
val mPath1 = Environment.getExternalStorageDirectory().toString() + "/watershed.png"
Imgcodecs.imwrite(mPath1,markers)
//Make the background black, and what we want to keep white
for (x in 0 until srcOriginal.rows()-1) {
for (y in 0 until srcOriginal.cols()-1) {
if(markers.get(x,y).get(0).equals(1.0)){
markers.put(
x,
y,
0.0
)
}
if((markers[x, y].get(0) == 255.0)){
markers.put(
x,
y,
255.0
)
}
}
}
//Use a kernel to dilate the image, to not lose any detail on the outline
//I used a kernel of 3x3 pixels
val marker_tempo = Mat()
val dilatation = Mat()
markers.convertTo(marker_tempo, CvType.CV_8U)
val kernel = Mat(3, 3, CvType.CV_8U)
Imgproc.dilate(marker_tempo, dilatation, kernel)
//Plot again to check whether the dilation is according to our needs
//If not, repeat by using a smaller/bigger kernel, or more/less iterations
val mPath2 = Environment.getExternalStorageDirectory().toString() + "/dilatation.png"
Imgcodecs.imwrite(mPath2,dilatation)
//Now apply the mask we created on the initial image
val final = Mat()
Core.bitwise_and(srcOriginal, srcOriginal, final, dilatation)
//Plot the final result
val mPath = Environment.getExternalStorageDirectory().toString() + "/final.png"
Imgcodecs.imwrite(mPath,final)
我目前正在python中使用Google Vision API检测图像中的汉字,但我发现Google将返回python源代码(如\XE7\X80\X86\XE7\XAB\X91),而不是一些人类可读字符串。 我如何将它转换成utf-8格式的人类可读文本? Requests.Exceptions.ConnectionError除外:打印(“Request Error”) 谢谢你
问题内容: 好的,所以我有一个来自EEG扫描的数据文件(一个二进制文件,data.eeg),在matlab中,用于读取文件并绘制部分数据的代码如下所示: 这是我的“翻译”尝试 这就是让我感到困惑的地方。根据文档,matlab的fread是一种通过fread(loaded_file,size,data_type)读取二进制文件的方法。python中的替代方法是使用numpy的fromfile并使用内
Python代码: 如何将上面在python中定义的代码转换为java?如果你能帮忙,我会很高兴的。
问题内容: 我刚刚开始使用Java 8,并且正在使用以下代码片段: 如何将其转换为Lambda样式? 问题答案: 如果是 功能界面 ,则可以 这是您问题中其他类的存根实现的完整示例:
问题内容: 我正在将Java库移植到C#。我使用的是Visual Studio 2008,因此没有停止使用的Microsoft Java语言转换助手程序(JLCA)。 我的方法是创建一个与Java库具有类似项目结构的新解决方案,然后将Java代码复制到ac#文件中,并将其逐行转换为有效的c#。考虑到我觉得Java易于阅读,两种语言之间的细微差别使我感到惊讶。 有些事情很容易移植(命名空间,继承等)
问题内容: 是否可以将Python程序转换为C / C ++? 我需要实现一些算法,而且我不确定性能差距是否足够大,足以证明我在C / C 中做的所有痛苦(我不擅长)。我考虑过要编写一种简单的算法,并针对这种转换后的解决方案进行基准测试。如果仅此一项比Python版本要快得多,那么除了在C / C 中做到这一点,我别无选择。 问题答案: 是。看看赛顿。它就是这样做的:将Python转换为C以加快速