Author: saqibkhan

  • Mobile Optimization:

    • Example: An e-commerce site might use responsive design to ensure its website is fully functional on mobile devices. This includes optimizing images for faster loading and making navigation easy on smaller screens.
  • Technical SEO:

    • Example: A website could improve its technical SEO by ensuring it has an XML sitemap, uses proper schema markup to enhance search results, and addresses any crawl errors found in Google Search Console to ensure all pages are indexed.
  • Backlink Building:

    • Example: A travel website might create valuable content like a detailed “Travel Itinerary for Italy” and reach out to travel bloggers and industry sites to link back to this resource, thereby improving its authority and search engine ranking.
  • Local SEO:

    • Example: A dental clinic in Los Angeles could optimize its Google My Business listing with accurate contact information, business hours, and local keywords like “dentist near downtown Los Angeles.” Encouraging satisfied patients to leave positive reviews would also help.
  • Content Creation:

    • Example: A tech blog might publish a comprehensive guide on “The Ultimate Guide to 5G Technology,” covering everything from what 5G is to how it compares with 4G, with in-depth information and visuals to rank well for related search queries.
  • Keyword Optimization

    • Example: A website for a local bakery could target keywords like “best cupcakes in [City]” and “gluten-free bakery [City]” to attract local customers searching for those specific products and services.
  • Enhancing Image Sharpness

    In this chapter we learn to increase the sharpness of an image using Gaussian filter.

    First we use OpenCV function GaussianBlur. It can be found under Imgproc package. Its syntax is given below −

    Imgproc.GaussianBlur(source, destination, new Size(0,0), sigmaX);
    

    The parameters are described briefly −

    Sr.No.Parameter & Description
    1sourceIt is source image.
    2destinationIt is destination image.
    3SizeIt is Gaussian kernel size.
    4sigmaXIt is Gaussian kernel standard deviation in X direction.

    Further, we use OpenCV function addWeighted to apply image watermark to image. It can be found under Core package. Its syntax is given below −

    Core.addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst);
    

    The parameters of this function are described below −

    Sr.No.Parameter & Description
    1src1It is first input array.
    2alphaIt is weight of the first array elements.
    3src2It is second input array of the same size and channel number as src1.
    4BetaIt is weight of the second array elements.
    5gammaIt is scalar added to each sum.
    6dstIt is output array that has the same size and number of channels as the input arrays.

    Apart from the GaussianBlur method, there are other methods provided by the Imgproc class. They are described briefly −

    Sr.No.Method & Description
    1cvtColor(Mat src, Mat dst, int code, int dstCn)It converts an image from one color space to another.
    2dilate(Mat src, Mat dst, Mat kernel)It dilates an image by using a specific structuring element.
    3equalizeHist(Mat src, Mat dst)It equalizes the histogram of a grayscale image.
    4filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta)It convolves an image with the kernel.
    5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)It blurs an image using a Gaussian filter.
    6integral(Mat src, Mat sum)It calculates the integral of an image.

    Example

    The following example demonstrates the use of Imgproc and Core class to apply sharpening to an image −

    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.Size;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgproc.Imgproc;
    
    public class Main {
       public static void main( String[] args ) {
    
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("digital_image_processing.jpg",
         Highgui.CV_LOAD_IMAGE_COLOR);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         Imgproc.GaussianBlur(source, destination, new Size(0,0), 10);
         Core.addWeighted(source, 1.5, destination, -0.5, 0, destination);
         Highgui.imwrite("sharp.jpg", destination);
      } catch (Exception e) {
      }
    } }

    Output

    When you execute the given code, the following output is seen −

    Original Image

    Sharped Image

  • Enhancing Image Contrast

    In this chapter learn how to enhance the contrast of an image using histogram equalization.

    We use the OpenCV function equalizeHist() method. It can be found under Imgproc package. Its syntax is given below −

    Imgproc.equalizeHist(source, destination);
    

    The parameters are described below −

    Sr.No.Parameter & Description
    1SourceIt is 8-bit single channel source image.
    2DestinationIt is the destination image.

    Apart from the equalizeHist() method, there are other methods provided by the Imgproc class. They are described briefly −

    Sr.No.Method & Description
    1cvtColor(Mat src, Mat dst, int code, int dstCn)It converts an image from one color space to another.
    2dilate(Mat src, Mat dst, Mat kernel)It dilates an image by using a specific structuring element.
    3equalizeHist(Mat src, Mat dst)It equalizes the histogram of a grayscale image.
    4filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta)It convolves an image with the kernel.
    5GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX)It blurs an image using a Gaussian filter.
    6integral(Mat src, Mat sum)It calculates the integral of an image.

    Example

    The following example demonstrates the use of Imgproc class to enhance contrast of an image −

    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgproc.Imgproc;
    
    public class Main {
    
       static int width;
       static int height;
       static double alpha = 2;
       static double beta = 50;
       
       public static void main( String[] args ) {
       
    
      try {
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source = Highgui.imread("grayscale.jpg", 
         Highgui.CV_LOAD_IMAGE_GRAYSCALE);
         Mat destination = new Mat(source.rows(),source.cols(),source.type());
         
         Imgproc.equalizeHist(source, destination);
         Highgui.imwrite("contrast.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("error: " + e.getMessage());
      }
    } }

    Output

    When you execute the given code, the following output is seen −

    Original Image

    Enhanced Contrast Image

  • GrayScale Conversion

    In order to convert a color image to Grayscale image, you need to read pixels or data of the image using File and ImageIO objects, and store the image in BufferedImage object. Its syntax is given below −

    File input = new File("digital_image_processing.jpg");
    BufferedImage image = ImageIO.read(input);	
    

    Further, get the pixel value using method getRGB() and perform GrayScale() method on it. The method getRGB() takes row and column index as parameter.

    Color c = new Color(image.getRGB(j, i));
    int red = (c.getRed() * 0.299);
    int green =(c.getGreen() * 0.587);
    int blue = (c.getBlue() *0.114);
    

    Apart from these three methods, there are other methods available in the Color class as described briefly −

    Sr.No.Method & Description
    1brighter()It creates a new Color that is a brighter version of this Color.
    2darker()It creates a new Color that is a darker version of this Color.
    3getAlpha()It returns the alpha component in the range 0-255.
    4getHSBColor(float h, float s, float b)It creates a Color object based on the specified values for the HSB color model.
    5HSBtoRGB(float hue, float saturation, float brightness)It converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.
    6toString()It returns a string representation of this Color.

    The last step is to add all these three values and set it again to the corresponding pixel value. Its syntax is given below −

    int sum = red+green+blue;
    Color newColor = new Color(sum,sum,sum);
    image.setRGB(j,i,newColor.getRGB());
    

    Example

    The following example demonstrates the use of Java BufferedImage class that converts an image to Grayscale −

    import java.awt.*;
    import java.awt.image.BufferedImage;
    
    import java.io.*;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    public class GrayScale {
    
       BufferedImage  image;
       int width;
       int height;
       
       public GrayScale() {
       
    
      try {
         File input = new File("digital_image_processing.jpg");
         image = ImageIO.read(input);
         width = image.getWidth();
         height = image.getHeight();
         
         for(int i=0; i<height; i++) {
         
            for(int j=0; j<width; j++) {
            
               Color c = new Color(image.getRGB(j, i));
               int red = (int)(c.getRed() * 0.299);
               int green = (int)(c.getGreen() * 0.587);
               int blue = (int)(c.getBlue() *0.114);
               Color newColor = new Color(red+green+blue,
               
               red+green+blue,red+green+blue);
               
               image.setRGB(j,i,newColor.getRGB());
            }
         }
         
         File ouptut = new File("grayscale.jpg");
         ImageIO.write(image, "jpg", ouptut);
         
      } catch (Exception e) {}
    } static public void main(String args[]) throws Exception {
      GrayScale obj = new GrayScale();
    } }

    Output

    When you execute the given example, it converts the image digital_image_processing.jpg to its equivalent Grayscale image and write it on hard disk with the name grayscale.jpg.

    Original Image

    Grayscale Image

  • Image Pixels

    An image contains a two dimensional array of pixels. It is actually the value of those pixels that make up an image. Usually an image could be color or grayscale.

    In Java, the BufferedImage class is used to handle images. You need to call getRGB() method of the BufferedImage class to get the value of the pixel.

    Getting Pixel Value

    The pixel value can be received using the following syntax−

    Color c = new Color(image.getRGB(j, i));
    

    Getting RGB Values

    The method getRGB() takes the row and column index as a parameter and returns the appropriate pixel. In case of color image, it returns three values which are (Red, Green, Blue). They can be get as follows−

    c.getRed();
    c.getGreen();
    c.getBlue();
    

    Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

    Getting Width and Height of Image

    The height and width of the image can be get by calling the getWidth() and getHeight() methods of the BufferedImage class. Its syntax is given below−

    int width = image.getWidth();
    int height = image.getHeight();
    

    Apart from these methods, there are other methods supported in the BufferedImage class. They are described briefly−

    Sr.No.Method & Description
    1copyData(WritableRaster outRaster)It computes an arbitrary rectangular region of the BufferedImage and copies it into a specified WritableRaster.
    2getColorModel()It returns ColorModel of an image.
    3getData()It returns the image as one large tile.
    4getData(Rectangle rect)It computes and returns an arbitrary region of the BufferedImage.
    5getGraphics()This method returns a Graphics2D, but is here for backwards compatibility.
    6getHeight()It returns the height of the BufferedImage.
    7getMinX()It returns the minimum x coordinate of this BufferedImage.
    8getMinY()It returns the minimum y coordinate of this BufferedImage.
    9getRGB(int x, int y)It returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.
    10getType()It returns the image type.

    Example

    The following example demonstrates the use of java BufferedImage class that displays pixels of an image of size (100 x 100)−

    import java.awt.*;
    import java.awt.image.BufferedImage;
    
    import java.io.*;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    class Pixel {
       BufferedImage image;
       int width;
       int height;
       
       public Pixel() {
    
      try {
         File input = new File("blackandwhite.jpg");
         image = ImageIO.read(input);
         width = image.getWidth();
         height = image.getHeight();
         
         int count = 0;
         
         for(int i=0; i<height; i++) {
         
            for(int j=0; j<width; j++) {
            
               count++;
               Color c = new Color(image.getRGB(j, i));
               System.out.println("S.No: " + count + " Red: " + c.getRed() +"  Green: " + c.getGreen() + " Blue: " + c.getBlue());
            }
         }
      } catch (Exception e) {}
    } static public void main(String args[]) throws Exception {
      Pixel obj = new Pixel();
    } }

    Output

    When you execute the above example, it would print the pixels of the following image −

    Original Image

    Understand Image Pixel Tutorial

    Pixels Output

    Understand Image Pixel Tutorial

    If you scroll down the ouput, the following pattern is seen−

    Understand Image Pixel Tutorial