//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中的定位代码转换为kotlin?
如何将python中的定位代码转换为kotlin?
推荐问题
分类汇总
- (2)
- .net(5)
- Access(210)
- android(1)
- android-studio(1)
- angular(1)
- bash(1)
- c(1)
- c#(625)
- chrome-devtools(1)
- CSS(782)
- css3动画(1)
- docker(1)
- docker-compose(2)
- dotnet(477)
- echarts5.0(1)
- elasticsearch(2)
- element-ui(1)
- eslint(1)
- eventbus(1)
- ffmpeg(2)
- fiddler(1)
- flask(1)
- flutter(1)
- git(2)
- Go(2093)
- golang(9)
- gradle(1)
- harmonyos(4)
- ios(1)
- Java(7682)
- javascript(1221)
- Jave(256)
- JS(330)
- jwt(1)
- kafka(1)
- linux(1)
- lua(1)
- matlab(1)
- mongodb(192)
- MySQL(2516)
- nestjs(1)
- nginx(1)
- Node(262)
- node.js(3)
- Oracle(458)
- php(1213)
- player(1)
- Postgres(167)
- ppt(1)
- python(11274)
- react.js(6)
- redis(2)
- rollup(1)
- seata(1)
- sequelize(1)
- sniffer(1)
- Solr(23)
- springboot(1)
- SQL(118)
- SQLServer(5624)
- Swift(224)
- sybase(21)
- typescript(5)
- uniapp(1)
- uni-app(1)
- vant-weapp(1)
- visual-studio-code(1)
- vue.js(12)
- vue3(3)
- vuex(1)
- wasm(1)
- webpack(1)
- 笔记本电脑(1)
- 调试技巧(1)
- 公众号(1)
- 机器学习(1)
- 计算机(1)
- 爬虫(1)
- 其他(33505)
- 前端(16)
- 算法(2)
- 小程序(3)
- 虚拟机(1)
- 运维(1)