简体   繁体   中英

How to read image in java like MATLAB imread()?

in Java I want to read an image. For this I have used this code:

BufferedReader br = ImageIO.read(new File('a.png'));

and it works.But values in br are like this: -16220000 , -5682000 ,... .

In MATLAB, imread() reads image to an array and the values are in the range 0 to 256. How imread() works?

In Java (like in C and C++ as well) you can use the OpenCV library. In this case the instruction you are looking for is:

include "opencv2/highgui/highgui.hpp"
include "iostream"
using namespace cv;
using namespace std;
int main(){
Mat img = imread("lena.png", CV_LOAD_IMAGE_COLOR);
    if (img.empty()){
    cout << "Cannot load image!" << endl;
    return -1;
    }
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", img);
waitKey(0);
return 0;
}
  • Here you can find the webpage with all the info on how to add these libraries.

  • Here you can find some tutorial and the whole documentation.

However, an image can be stored in many different ways. Once you have the image in your memory heap, you can work with the values and, so, fix the image-pixel-values in a rage that you prefer (in this case [0,255]).

For example, if you have a pixel in the range [0,X] and you want to map it in a range [0,255], the formula is pixel_value/X*255 . You can use a 'for' to iterate on the total number of pixels of your input image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM