Tag Archives: matlab

[Solved] python MATLAB Error: OSError: MATLAB Engine for Python supports Python version 2.7, 3.4, 3.5 and3.6, but your version of python is 3.8

Because the coordinates of broken line graph and curve graph are to be read recently, Python is used to call matlab to try, but an error is reported in this process:

The matlab2017b I use does not support Python 3 Version 8.

Solution:

I chose to create a virtual environment in anaconda.

As follows:

Because the maximum version of 3.6 is supported, a version of 3.6 is created

Open pycharm, set it in setting, and select Add

Generally, the virtual environment created is in the envs of anaconda

After setting the environment, it can be executed successfully again

Test:

Run successfully

The use of try… Catch in MATLAB

The use of try… Catch in MATLAB

In the design of MATLAB program, if we can’t ensure whether a certain program code will make mistakes, we can use try… Catch statement, which can capture and handle errors, so that the possible error code will not affect the subsequent code execution, and can also check

Check, solve some errors of the program, enhance the robustness and reliability of the code

Format:

try

Program code 1

catch

Program code 2

end

First, the program runs “code 1” between try and catch. If there is no error, it does not execute “code 2” between catch and end, but executes the program after end; If an error occurs when executing “program code 1”, execute “program code 2” immediately, and then continue to execute the program after end

For example:

1、try…end

Try… End is used to try to run a piece of code that may go wrong, such as:

m = rand(3,4);

n = magic(5);

try

a = m*n;

disp(a)

end

disp(m)

In this code, a = m * n will run in error, which does not meet the principle of matrix multiplication. Therefore, a = m * N and disp (a) will not be executed, but the subsequent disp (m) will also be executed;

2、try…catch…end

m = rand(3,4);

n = magic(5);

try

a = m*n;

disp(a)

catch err

disp(size(m))

disp(size(n))

end

disp(m)

Here, when the program encounters a = m * n; After the error, it will jump to the statement in the catch and continue to execute, which is similar to if… Else… End;

The addition of err enables it to clearly display the line where debugging is running

Copyright notice: This article is the original article of the blogger and cannot be reproduced without the permission of the blogger. https://blog.csdn.net/wangrenbao123/article/details/55252242

Image data type conversion uint8 and double in MATLAB

The image pixel data is processed in MATLAB

img1=double(imread(‘lenna.bmp’));

Imshow pictures in MATLAB should be converted into uint8: 1

subplot(1,2,1),imshow(uint8(img1)),title(‘original’);
subplot(1,2,2),imshow(uint8(img2)),title(‘after’);


MATLAB image processing on Unit8_ Baidu Knows https://zhidao.baidu.com/question/545122188.html

In order to save storage space, matlab provides a special data type uint8 (8-bit unsigned integer) for image, and the image stored in this way is called 8-bit image
imread stores gray images in an 8-bit matrix. When it is an RGB image, it is stored in an 8-bit RGB matrix

therefore, the image data read in MATLAB is uint8, and the values in MATLAB are generally stored and calculated in double (64 bit). Therefore, it is necessary to convert the image to double format before operation,
I2 = im2double (I1)% convert image I1 to double precision type (assuming graph matrix range 0-255)
or
i64 = double (I8)/255% uint is converted to double
if it is not converted, the calculation will overflow


MATLAB image type conversion and uint8, double, im2double, im2uint8 and mat2gray and other instructions – Thomas can write – CSDN blog https://blog.csdn.net/kakiebu/article/details/78959249

1. MATLAB image saving instructions

The data saved after reading pictures in MATLAB is uint8 (8-bit unsigned integer, i.e. 1 byte). The image stored in this way is called 8-bit image. Compared with the default MATLAB data type, double precision floating-point double (64 bits, 8 bytes), it can naturally save a lot of storage space
in detail, imread stores gray-scale images in an 8-bit matrix. When it is an RGB image, it stores it in an 8-bit RGB matrix. For example, if the pixel size of a color image is 400 * 300 (height * width), the saved data matrix is 400 * 300 * 3, where each color channel value is between 0 and 255

Function ‘*’ is not defined for values of class ‘uint8’

One byte unsigned integer can only store 255 data at most, so it is easy to overflow the image operation

2. MATLAB image type conversion

The image data read by Matlab is uint8, and the values in MATLAB are usually stored and calculated by double (64 bit). Therefore, it is necessary to convert the image to double format before operation. The differences are as follows:

img = imread(‘./1.jpg’); % Read in is Unit8 (0 ~ 255) data

I1 = im2double(img); % Convert image to double precision type (0 ~ 1)

I2 = double(img)/255; % Uint8 is converted to double, which is the same as im2double

Here is a supplement to explain the difference between im2double() and double(). Double (IMG) is a simple data type conversion, which converts an unsigned integer to a double precision floating-point double, but the data size does not change. The original data is between 0 and 255, but after conversion, it is still 0 to 255. For example, if the original value is 255, then it will be 255.0 after conversion. The number of decimal places 0 is determined by the length of double data. The actual data size is 255, but this 255 is already a double type space storage, and no overflow will occur if it is added. Im2double (IMG) not only converts uint8 to double, but also maps the data size from 0 to 255 to 0 to 1