Does matlab supports imgcodes of openCV
13 views (last 30 days)
Show older comments
Iam trying to use cv2.imwrite in jpeg format for which imagecodes of opencv is required. Ultimately want to use the C++ code using openCV for convertion to matlab readable format using mexOpenCV function.
I have installed "computer vision toolbox interface for OpenCV in Matlab". But the conversion throws error because when i check the precences of imgcodes.hpp inside opencv2 folder its not present.
The automatically generated file opencv_modules.hpp also does not list down imgcodecs
1 Comment
SACHIN KHANDELWAL
on 19 Jan 2025
This MATLAB Answer post might be helpful for a heads up : https://www.mathworks.com/matlabcentral/answers/439586-how-to-generate-mex-for-opencv-s-imdecode-for-cpu-and-gpu
Answers (1)
Broy
on 4 Sep 2025 at 6:57
Hi Manmeet,
It looks like you are trying to use OpenCV’s cv::imwrite (via the Computer Vision Toolbox Interface for OpenCV in MATLAB) to save an image in JPEG format. The issue arises because the OpenCV version included with MATLAB does not have the imgcodecs module, which is required for functions like imwrite. That’s why you don’t see imgcodecs.hpp or the module listed in opencv_modules.hpp.
To resolve this, you have two potential options:
1. Simplest: Use MATLAB’s own functions for image I/O.
MATLAB already supports saving images in JPEG directly. For example:
img = imread('sample_img.png');
imwrite(img, 'output.jpg');
This avoids the need for OpenCV completely.
2. If you specifically need OpenCV’s imwrite inside a MEX function:
You will need a full OpenCV build that includes the imgcodecs module. MATLAB’s packaged OpenCV does not include it. In that case, you’d have to install OpenCV separately and link your MEX code against it.
Helpful Documentations you can refer to:
- https://www.mathworks.com/help/matlab/ref/imwrite.html
- https://docs.opencv.org/4.x/d4/da8/group__imgcodecs.html
- https://docs.opencv.org/4.x/d7/d9f/tutorial_linux_install.html
- https://www.mathworks.com/help/matlab/ref/mex.html
Hope this helps.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!