PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Ahmed Abdulla   PHP Grayscale Image with OpenCV   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Main Code
Class: PHP Grayscale Image with OpenCV
Convert an image to a grayscale using OpenCV
Author: By
Last change:
Date: 1 year ago
Size: 678 bytes
 

Contents

Class file image Download
<?php

// Load OpenCV extension
if (!extension_loaded('opencv')) {
   
dl('opencv.so'); // Load the OpenCV extension dynamically
}

// Load the image
$imagePath = 'path/to/your/image.jpg'; // Change this to the actual path of your image
$image = cv\imread($imagePath, cv\IMREAD_COLOR);

if (
$image === null) {
    die(
'Could not load image');
}

// Convert the image to grayscale
$grayImage = cv\cvtColor($image, cv\COLOR_BGR2GRAY);

// Save the grayscale image
$grayImagePath = 'path/to/save/gray_image.jpg'; // Change this to the desired path for the output image
cv\imwrite($grayImagePath, $grayImage);

echo
'Image processed and saved as grayscale.';