使用opencv和python 3.7显示图像时遇到麻烦

时间:2019-07-05 21:25:07

标签: python python-3.x opencv

我正在尝试使用以下代码在python 3.7中显示带有openCV 4.1的图像:

     public ImageButton play, stop, record;

     MediaRecorder myAudioRecorder;

     String outputFile;




    boolean refreshed=false;
    public static Typeface tf;
    ImageButton infoButton;
    ImageButton refreshButton;
    TextView minVal;
    TextView maxVal;
    TextView mmVal;
    TextView curVal;
    long currentTime=0;
    long savedTime=0;
    boolean isChart=false;
    boolean isMoney=false;
    /* Decibel */
    private boolean bListener = true;
    private boolean isThreadRun = true;
    final int REQUEST_PERMISSION_CODE=1000;
    final int AUDIO_RECORDING_DELAY=1000;
    private Thread thread;
    float volume = 10000;
    int refresh=0;
    private MyMediaRecorder mRecorder ;
    boolean x=false;
    final World world=new World();
    DatabaseReference mref;
    Button shoeScore;


    final Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            DecimalFormat df1 = new DecimalFormat("####.0");
            if(msg.what == 1){



                double x=World.maxDB*100;
                maxVal.setText(df1.format(x));
                curVal.setText(df1.format(World.dbCount));

                if(refresh==1){
                    long now=new Date().getTime();
                    now=now-currentTime;
                    now=now/1000;
                    refresh=0;
                }else {
                    refresh++;
                }
            }
        }
    };

    @Override
    protected void onStart() {
        if (world.hello){
            x=true;
            startListenAudio();

        }
        super.onStart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);




        if (!checkPermissionFromDevice()){
            requets_Permission();
        }










        // HERE START CODE

        play = (ImageButton) findViewById(R.id.playButton);
        stop = (ImageButton) findViewById(R.id.stopButton);
        record = (ImageButton) findViewById(R.id.recordButton);

            stop.setEnabled(false);


            outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

            myAudioRecorder = new MediaRecorder();
            myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
            myAudioRecorder.setOutputFile(outputFile);


            record.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {

                        myAudioRecorder.prepare();
                        myAudioRecorder.start();
                    } catch (IllegalStateException ise) {
                        // Make something
                    } catch (IOException ioe) {
                        //make something
                    }

                    record.setEnabled(false);
                    stop.setEnabled(true);


                    Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
                }

            });

            stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    myAudioRecorder.stop();
                    myAudioRecorder.release();
                    myAudioRecorder = null;
                    record.setEnabled(true);

                    stop.setEnabled(false);
                    play.setEnabled(true);

                    Toast.makeText(getApplicationContext(), "Audio recorded sucessfully", Toast.LENGTH_LONG).show();
                }
            });


            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MediaPlayer mediaPlayer = new MediaPlayer();

                    try {
                        mediaPlayer.setDataSource(outputFile);
                        mediaPlayer.prepare();
                        mediaPlayer.start();

                        Toast.makeText(getApplicationContext(), "Playing Audio", Toast.LENGTH_LONG).show();


                    } catch (Exception e) {
                        //Make something

                    }


                }
            });

但是它给了我以下错误信息:

import cv2
img_path = 'Path/to/image.tiff'
img = cv2.imread(img_path)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

我在做什么错了?

编辑

这似乎不是权限问题,或者文件不存在,因为我可以使用带有matplotlib的以下代码来显示图像

error: OpenCV(4.1.0) /tmp/opencv-20190505-12101-14vk1fh/opencv-4.1.0/modules/highgui/src/window.cpp:352: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

编辑2

尝试使用import matplotlib.pyplot as plt img_path = 'Path/to/image.tiff' img = plt.imread(img_path) fig = plt.figure(0) plt.imshow(img) plt.show() 软件包

tifffile

我现在收到以下错误消息:

import tifffile as tiff
a = tiff.imread(img_path)
a.shape

尝试Traceback (most recent call last): File "opencv_script.py", line 16, in <module> a = tiff.imread(img_path) File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/tifffile/tifffile.py", line 720, in imread return tif.asarray(**kwargs) File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/tifffile/tifffile.py", line 2405, in asarray maxworkers=maxworkers) File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/tifffile/tifffile.py", line 4539, in asarray strip = unpack(strip) File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/tifffile/tifffile.py", line 4491, in unpack runlen) File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/tifffile/tifffile.py", line 10306, in packints_decode % (numbits, numpy.dtype(dtype))) NotImplementedError: unpacking 12-bit integers to uint16 not supported 软件包:

libdif

我得到:

import libtiff
# img_path = "/Users/mymac/Documents/research/In-Vitro_AK_Studie/boarder_tracking_py/line.jpg"
img_path = '/Users/mymac/Documents/research/In-Vitro_AK_Studie/boarder_tracking_py/Camera No.1_C001H001S0001000561.tif'
# img_path_save = '/Users/mymac/Documents/research/In-Vitro_AK_Studie/boarder_tracking_py/modif.tif'
# img = plt.imread(img_path)
# threshold the image
# img.shape
# fig = plt.figure(0)
# plt.imshow(img)
# plt.show()

tif = libtiff.TIFF.open(img_path)
im = tif.read_image()

然后我安装了Traceback (most recent call last): File "opencv_script.py", line 18, in <module> im = tif.read_image() File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/libtiff/libtiff_ctypes.py", line 552, in read_image typ = self.get_numpy_type(bits, sample_format) File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/libtiff/libtiff_ctypes.py", line 509, in get_numpy_type raise NotImplementedError("bits = {0:d}".format(bits)) NotImplementedError: bits = 12 ,但收到了以下错误消息:

imagecodecs-lite

我上传了有问题的文件on github

编辑3(工作代码)

Traceback (most recent call last):
  File "opencv_script.py", line 19, in <module>
    im = tif.read_image()
  File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/libtiff/libtiff_ctypes.py", line 552, in read_image
    typ = self.get_numpy_type(bits, sample_format)
  File "/Users/mymac/miniconda3/envs/pyimageprocessing/lib/python3.7/site-packages/libtiff/libtiff_ctypes.py", line 509, in get_numpy_type
    raise NotImplementedError("bits = {0:d}".format(bits))
NotImplementedError: bits = 1

0 个答案:

没有答案