RSS
 

PHP: Find string between 2 strings

14 Jun

While looking in the Internet for a good function to strip a string, I found this blog where this function is implemented.
[code brush="php"] function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}

$fullstring = "this is my [tag]dog[/tag]";
$parsed = get_string_between($fullstring, "[tag]", "[/tag]");
echo $parsed; // (result = dog)
[/code]

 
No Comments

Posted in PHP

 

PHP: Read file line-by-line

13 Jun

[code="php"]
< ?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.

$lines = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
}
?>
[/code]

 
No Comments

Posted in PHP

 

OpenCV: Image allignment Algorithms

11 Jun

I found a good article where Image allignment Algorithms are discussed with source code.
In this article we see:

  • Forwards Additive Algorithm
  • Inverse Compositional Image Alignment Algorithm
  • Bilinear Interpolation

Check it here


 
2 Comments

Posted in OpenCV

 

OpenCV: Converting an image to gray scale

11 Jun

This is an easy way to convert 3 channel image which means color image:

// convert to gray scale
IplImage* img_gray = cvCreateImage( cvSize(img_rgb->width, img_rgb->height), IPL_DEPTH_8U, 1 );
cvCvtColor( img_rgb, img_gray, CV_BGR2GRAY );

The standard color sapce in OpenCV is BGR.

 
2 Comments

Posted in OpenCV

 

OpenCV: The cross correlation of two opencv images

11 Jun

This function calculates the cross correlation between two images of the type IplImage*. Returns the resulting value.

double cross_correlation( IplImage* img1, IplImage* img2 ){
  double corr;

  int M = img1->width;
  int N = img1->height;

  BwImage img_1( img1 ); // using opencv wrapper for accessing image pixels
  BwImage img_2( img2 );

  CvScalar img1_avg = cvAvg( img1, NULL );
  CvScalar img2_avg = cvAvg( img2, NULL );

  double sum_img1_img2 = 0;
  double sum_img1_2 = 0;
  double sum_img2_2 = 0;

  for( int m=0; m<m ; ++m ){
   for( int n=0; n<n; ++n ){
    sum_img1_img2   = sum_img1_img2 + (img_1[m][n]-img1_avg.val[0])*(img_2[m][n]-img2_avg.val[0]);
    sum_img1_2      = sum_img1_2 + (img_1[m][n]-img1_avg.val[0])*(img_1[m][n]-img1_avg.val[0]);
    sum_img2_2      = sum_img2_2 + (img_2[m][n]-img2_avg.val[0])*(img_2[m][n]-img2_avg.val[0]);
    }
  }

  corr = sum_img1_img2/sqrt(sum_img1_2*sum_img2_2);

  return corr;
}
 
7 Comments

Posted in OpenCV