MATLAB – IMAGE PROCESSING

Photo of author

By Jackson Taylor

Home » Examples » Open Cv » MATLAB – IMAGE PROCESSING

As an Amazon Associate we earn from qualifying purchases.

MATLAB is widely used for image processing because it treats an image as what it really is: a matrix of numbers. Once a picture is loaded, you can filter it, convert it, measure it or analyze it with the same matrix operations you use for any other data. This page is a starting point. It explains the basic ideas and links to step-by-step examples for displaying images, converting color to grayscale, detecting edges and working with a live camera.

How MATLAB Sees an Image

A grayscale image is a two-dimensional matrix. Each element is one pixel, and its value is the brightness at that point. A color (RGB) image is a three-dimensional array, with one layer each for red, green and blue.

Most images are stored as 8-bit unsigned integers (type uint8), so pixel values run from 0 (black) to 255 (white). Some functions convert images to double precision, where values usually fall between 0 and 1. If your result looks completely white or black, a mismatch between these two ranges is a common cause.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

What You Need

  • MATLAB. Basic reading, writing and displaying of images works in core MATLAB.
  • The Image Processing Toolbox, which adds many of the functions used below, such as edge detection, filtering and morphology. Check which functions your installation includes before you start.
  • A few test images in a common format such as JPEG or PNG, saved in your current folder or on the MATLAB path.

BASIC EXAMPLES

The three examples below cover the steps almost every image processing task begins with. Work through them in order.

  1. DISPLAYING AN IMAGE
  2. CONVERTING RGB (COLOR) IMAGE TO GRAY SCALE IMAGE
  3. DETECTING EDGES OF PICTURE

1. Reading and displaying an image

The function imread loads an image file into a variable, and imshow shows it in a figure window. For example, I = imread(‘myphoto.jpg’); imshow(I); is enough to see your picture. The function size tells you the dimensions. A result with three numbers means a color image, and two numbers means grayscale.

See also
ADC0808

2. Converting RGB to grayscale

Many algorithms work on brightness only, so color images are often converted to grayscale first. The function rgb2gray does this by combining the three color layers into one. Working with a single layer is faster and makes later steps, such as thresholding and edge detection, simpler.

3. Detecting edges

Edges are places where brightness changes sharply, and they usually mark the outline of objects. The edge function finds them in a grayscale image and returns a black-and-white image where edge pixels are white. It offers several methods, including Sobel, Prewitt, Roberts and Canny. Canny often gives thinner, cleaner lines, while Sobel is a simple, fast choice.

Other Common Operations

After the basics, these are the operations you are most likely to need next:

  • Resizing and rotating: imresize and imrotate change image size and orientation.
  • Noise removal: imgaussfilt applies a Gaussian blur, and medfilt2 applies a median filter, which works well against salt-and-pepper noise.
  • Contrast adjustment: imadjust stretches the intensity range, and histeq spreads out the histogram to improve contrast.
  • Thresholding: imbinarize turns a grayscale image into black and white, which helps separate an object from its background.
  • Morphology: imerode, imdilate, imopen and imclose clean up shapes in binary images.
  • Measuring objects: regionprops reports properties such as area and centroid for each object found in a binary image.
  • Saving results: imwrite saves a processed image to a file.

A typical workflow is: read the image, convert to grayscale, remove noise, threshold or detect edges, then measure or save the result. Display the image after each step so you can see what each function actually does.

REAL TIME IMAGE PROCESSING

Real-time processing means working on images as they arrive from a camera instead of from saved files. In MATLAB, a USB webcam can be connected through the MATLAB Support Package for USB Webcams. After that you create a camera object and grab frames with the snapshot function. Each frame is an ordinary image matrix, so every function described above works on it.

See also
AD620

The example below shows how to capture images from a USB webcam in MATLAB and display the frame.

  1. how to capture an image using usb webcamera in matlab

If you want to process video continuously, place the capture and processing steps inside a loop. Keep the processing light, because heavy filtering on every frame can make the display lag. Reducing the image size with imresize is an easy way to speed things up.

Tips for Better Results

  • Check the data type of your image with class before doing arithmetic. Adding numbers to a uint8 image can saturate at 255.
  • Process a copy of the image so you always keep the original for comparison.
  • Use consistent lighting when you capture images with a camera. Poor or changing light causes more problems than most algorithms can fix.
  • Try a few threshold or filter settings and compare them visually instead of relying on one default value.

MATLAB Compared with Other Tools

MATLAB is convenient for learning and prototyping because the functions are well documented and results are easy to display. It is not the only option. Python with OpenCV is a popular free alternative, and the same ideas carry over. If you want to compare, see our notes on opening an image in Python and writing a new image in Python. For a hardware-oriented direction, our article on machine learning for image classification on Arduino shows another way images can be analyzed.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works

FAQ

Do I need the Image Processing Toolbox?

Not for the very basics. Functions such as imread, imshow and imwrite work in core MATLAB. Many of the more advanced functions, including edge, imbinarize and regionprops, belong to the Image Processing Toolbox.

Why do I convert an image to grayscale?

Grayscale reduces the data from three layers to one, which makes processing faster and simpler. Many techniques, such as edge detection and thresholding, only need brightness information.

See also
3 phase 3 level inverter simulation in matlab

Can MATLAB process images from a webcam?

Yes. With the USB webcam support package installed, MATLAB can connect to a supported camera and capture frames. Each frame can then be processed like any other image.

Which image formats does MATLAB support?

MATLAB reads common formats including JPEG, PNG, BMP and TIFF. The imfinfo function shows details about a file, and imformats lists the formats your installation supports.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works