Open In App

What is Oil Painting in MATLAB?

Last Updated : 20 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this era of information and intelligence, it seems that painting graphics have been seriously marginalized, which also produced how to better develop painting technology in modern society. Nowadays, in this field, digital image processing plays important role in the expansion of oil painting creation with technical improvement.  Oil painting is a kind of art. It is a kind of art that is not only beautiful but also useful. And today, in this article, we will discuss how to do oil painting in MATLAB.

In MATLAB, oil painting comes under the category of image processing. Here are some of the steps that we need to follow to convert a simple image into an oil painting.

We have to define a small window matrix of any arbitrary size m x n. Then copy the original image into the window matrix. And we will find the histogram of each value. Then we have to replace the maximum occurring pixel value in the window matrix with the new pixel value. Finally, we have to copy the window matrix into the original image.

Approach:

Reading images: First, the input image is read and stored in a variable named img. MATLAB uses the imread function to open and read the image and load it into a variable. In the given example, we are loading the image named “image.jpg” into the variable “I”.

I=imread('image.jpg');

Then we have to define a small window matrix of any arbitrary size m x n. Then copy the original image into the window matrix. And we will find the histogram of each value. Then we have to replace the maximum occurring pixel value in the window matrix with the new pixel value.

Showing images: Original and modified images are displayed. Images are shown into the MATLAB environment using the imshow() function.

imshow(I);

Where “i”, is the image array and the image is displayed.

Example:

Matlab




% MATLAB code for Oil Painting
% reading the image
img = imread('GeeksforGeeks.jpg');
  
% defining the window matrix
m = 6;
n = 7;
wimg = uint8(zeros([size(img,1)-m,size(img,2)-n,3]));
 
% calculating the histogram for each RGB channel
for p = 1:3
for i = 1:size(img,1) - m
    for j = 1:size(img,2) - n
       mask = img(i:i+m-1,j:j+n-1,p);
       his = zeros(1,256);
       for k = 1:(m*n)
           his(mask(k)+1) = his(mask(k)+1) + 1;
       end
        
% maximum occurring pixel value
[max_val,max_index] = max(his);
 
% replacing the maximum occurring pixel value with the new pixel value
         wimg(i,j,p) = max_index - 1;
     end
    end
    end
 
% displaying image
figure,imshow(wimg);


Output:

Figure: Original Image

Figure: Oil painting


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads