首页 > 解决方案 > 使用 Canvas 正确绘制点

问题描述

我有一个 python 脚本,它使用 OpenCV 从 ckpts 文件(模型)中获取图像输入,在眼睛区域的轮廓上绘制地标。我想在同一张图片中绘制这些点(地标)。我从图片中得到预测点,并尝试使用 Canvas 绘制这些点 (x,y),但结果不同。两张图的区别:

使用 python 脚本 (OpenCV) 绘制地标

使用 Java 代码(Canvas)绘制地标

我尝试了很多方法,我使用 Canvas 库在 imageview 上绘制点(我在 assets 文件夹中加载了相同的图像)但这并不能解决我的问题..

这是一个 Python 代码,展示了如何在图像上绘制地标:

predictions = estimator.predict(input_fn=_predict_input_fn)
        for _, result in enumerate(predictions):
            img = cv2.imread(result['name'].decode('ASCII') + '.jpg')
            print(result['logits'])
            print(result['name'])
            marks = np.reshape(result['logits'], (-1, 2)) * IMG_WIDTH
            print("reshape values  "+str(np.reshape(result['logits'], (-1,2))))
            print("marks  "+str(marks))

            for mark in marks:
                cv2.circle(img, (int(mark[0]), int(
                    mark[1])), 1, (0, 255, 0), -1, cv2.LINE_AA)
            try:
                img = cv2.resize(img, (512, 512))
                cv2.imshow('result', img)
            except Exception as e:
                print(str(e))
           # output_node_names = [n.name for n in tf.get_default_graph().as_graph_def().node]
           # print(output_node_names)
            cv2.waitKey()

此文件显示来自 python 代码的打印日志:


[0.33135968 0.19592011 0.34212315 0.17297666 0.36624995 0.16413747
 0.3894139  0.17440952 0.39828074 0.1978043  0.3891497  0.22268474
 0.36345637 0.22974193 0.3401759  0.2193309  0.30167252 0.20411113
 0.3167112  0.19134495 0.33793524 0.18388326 0.3642417  0.18049955
 0.3903508  0.18533507 0.40906873 0.1957745  0.42142123 0.21091096
 0.40550107 0.21829814 0.38345626 0.22071144 0.35900232 0.22142673
 0.3363348  0.21877256 0.3161971  0.2133534  0.62843406 0.21482795
 0.6389724  0.1914106  0.6628249  0.1835615  0.6858679  0.19583184
 0.6946868  0.22111627 0.6840309  0.24444285 0.66027373 0.25241333
 0.6351568  0.24192403 0.60499936 0.22642238 0.6210091  0.21289764
 0.6423563  0.2042976  0.6685919  0.20277795 0.69201195 0.20948553
 0.70882106 0.22015369 0.71931773 0.23518339 0.7076659  0.24166131
 0.69054717 0.24350837 0.6694564  0.24258481 0.64537776 0.23927754
 0.62199306 0.23511863]
b'C:\\Users\\*******\\cnn-facial-landmark\\targetiris\\irisdata-300VW_Dataset_2015_12_14-017-000880'
reshape values  [[0.33135968 0.19592011]
 [0.34212315 0.17297666]
 [0.36624995 0.16413747]
 [0.3894139  0.17440952]
 [0.39828074 0.1978043 ]
 [0.3891497  0.22268474]
 [0.36345637 0.22974193]
 [0.3401759  0.2193309 ]
 [0.30167252 0.20411113]
 [0.3167112  0.19134495]
 [0.33793524 0.18388326]
 [0.3642417  0.18049955]
 [0.3903508  0.18533507]
 [0.40906873 0.1957745 ]
 [0.42142123 0.21091096]
 [0.40550107 0.21829814]
 [0.38345626 0.22071144]
 [0.35900232 0.22142673]
 [0.3363348  0.21877256]
 [0.3161971  0.2133534 ]
 [0.62843406 0.21482795]
 [0.6389724  0.1914106 ]
 [0.6628249  0.1835615 ]
 [0.6858679  0.19583184]
 [0.6946868  0.22111627]
 [0.6840309  0.24444285]
 [0.66027373 0.25241333]
 [0.6351568  0.24192403]
 [0.60499936 0.22642238]
 [0.6210091  0.21289764]
 [0.6423563  0.2042976 ]
 [0.6685919  0.20277795]
 [0.69201195 0.20948553]
 [0.70882106 0.22015369]
 [0.71931773 0.23518339]
 [0.7076659  0.24166131]
 [0.69054717 0.24350837]
 [0.6694564  0.24258481]
 [0.64537776 0.23927754]
 [0.62199306 0.23511863]]
marks  [[37.112286 21.943052]
 [38.317795 19.373386]
 [41.019993 18.383396]
 [43.614357 19.533867]
 [44.607445 22.154081]
 [43.584766 24.940691]
 [40.707115 25.731096]
 [38.0997   24.565062]
 [33.787323 22.860447]
 [35.471653 21.430634]
 [37.848747 20.594925]
 [40.79507  20.21595 ]
 [43.719288 20.757528]
 [45.815697 21.926743]
 [47.199177 23.622028]
 [45.41612  24.44939 ]
 [42.9471   24.71968 ]
 [40.20826  24.799793]
 [37.6695   24.502527]
 [35.414074 23.89558 ]
 [70.38461  24.06073 ]
 [71.56491  21.437988]
 [74.23639  20.558887]
 [76.81721  21.933167]
 [77.80492  24.765022]
 [76.61146  27.3776  ]
 [73.95066  28.270294]
 [71.137566 27.095491]
 [67.759926 25.359306]
 [69.553024 23.844536]
 [71.9439   22.881332]
 [74.88229  22.71113 ]
 [77.50534  23.46238 ]
 [79.387955 24.657213]
 [80.56358  26.34054 ]
 [79.25858  27.066067]
 [77.341286 27.272938]
 [74.97912  27.169498]
 [72.28231  26.799084]
 [69.66322  26.333286]]

Java 代码 (Android)

  private void drawpoint(ImageView imageView,float x,float y, int raduis){
        myOptions.inDither = true;
        myOptions.inScaled = false;
        myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
        myOptions.inPurgeable = true;
        canvas.drawCircle(x,y, raduis, paint);
        imageView = (ImageView)findViewById(R.id.imageView);
        imageView.setAdjustViewBounds(true);
        imageView.setImageBitmap(mutableBitmap);
    }
         drawpoint(image2,  38,  19,1);
            drawpoint(image2,41,18,1);
            drawpoint(image2,43,19,1);
            drawpoint(image2,40,25,1);
            drawpoint(image2,38,24,1);

我怎么解决这个问题?

标签: javapythonandroidopencvcanvas

解决方案


问题解决了。我使用 OpenCV 库而不是 Canvas 库在 Android 中绘图。我已经使用了这个功能: Imgproc.circle()


推荐阅读