Sunday, March 31, 2013

How to use Order Statistic Filters with OpenCV

[caption id="attachment_789" align="alignnone" width="471"]Order Statistic Filters Order Statistic Filters[/caption]

Order Statistic filters are filters whose response is based on ordering/ranking the pixels
containing in the 3x3 window.
1. Median filter

The value of the centre pixel is replaced by the median value of its neighbourhood pixels. The median is taken after arranging the pixel values in ascending order and then taking the middle value. Use the following function,
void cvSmooth( const CvArr* src,
CvArr* dst,
int smoothtype=CV_MEDIAN,
int param1=3, int param2=3);

CV_MEDIAN (median blur) - finding median of param1×param1 neighborhood (the neighborhood is square).
2. Max filter
The max filter uses the maximum value of its neighbourhood pixels to replace the centre pixel value.
void cvDilate(const CvArr* src,
CvArr* dst,
IplConvKernel* element=NULL,
int iterations=1)

The function dilates the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the maximum is taken. Image Understanding and Processing (OpenCV)

3. Minimum filter
The minimum filter uses the minimum value of its neighbourhood pixels to replace the centre pixel value.
void cvErode(const CvArr* src,
CvArr* dst,
IplConvKernel* element=NULL,
int iterations=1)

The function erodes the source image using the specified structuring element that determines the shape of a pixel neighborhood over which the minimum is taken.

Sample Code

[sourcecode language="cpp"]
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

int _tmain(int argc, _TCHAR* argv[])
{

IplImage *img = cvLoadImage("apple noise.jpg");
IplImage *dst1=cvCreateImage(cvSize(img->width,img->height),8,3);

//cvSmooth(img,dst,CV_MEDIAN,3,3); Median Filter
//cvDilate(img,dst,NULL,1); Max Filter
//cvErode(img,dst,NULL,1); //Max FiltercvNamedWindow

cvNamedWindow("Image:");
cvShowImage("Image:", img);

cvNamedWindow("DST:");
cvShowImage("DST:", dst1);

cvWaitKey(0);

cvDestroyWindow("Image:");
cvReleaseImage(&img);

cvDestroyWindow("DST:");
cvReleaseImage(&dst1);

return 0;
}

[/sourcecode]

Wednesday, March 20, 2013

How to get TimeZone Offset in PHP

[caption id="attachment_768" align="alignnone" width="502"]timezone timezone[/caption]

[sourcecode language="php"]
function getOffsetByTimeZone($localTimeZone)
{
$time = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone($localTimeZone));
$timezoneOffset = $time->format('P');
return $timezoneOffset;
}

[/sourcecode]

Call this function passing a local time zone as a string.
eg.
getOffsetByTimeZone('America/New_York');

Saturday, March 16, 2013

How to switch between Timezones in PHP

[caption id="attachment_768" align="alignnone" width="502"]timezone timezone[/caption]

[Extracted from : http://www.mindfiresolutions.com/PHP-function-for-Time-Zone-conversion-56.php]

Convert from GMT to a local timezone

[sourcecode language="php"]
function ConvertGMTToLocalTimezone($gmttime,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();

date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");

$local_timezone = $timezoneRequired;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");

date_default_timezone_set($system_timezone);
$diff = (strtotime($local) - strtotime($gmt));

$date = new DateTime($gmttime);
$date->modify("+$diff seconds");
$timestamp = $date->format("Y-m-d h:i:s A");
return $timestamp;

}
[/sourcecode]

Convert from local timezone to GMT

[sourcecode language="php"]
function ConvertLocalTimezoneToGMT($gmttime,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();

$local_timezone = $timezoneRequired;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");

date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");

date_default_timezone_set($system_timezone);
$diff = (strtotime($gmt) - strtotime($local));

$date = new DateTime($gmttime);
$date->modify("+$diff seconds");
$timestamp = $date->format("Y-m-d h:i:s A");
return $timestamp;
}

[/sourcecode]

Convert from one local timezone to another local timezone


[sourcecode language="php"]
function ConvertOneTimezoneToAnotherTimezone($time,$currentTimezone,$timezoneRequired)
{
$system_timezone = date_default_timezone_get();
$local_timezone = $currentTimezone;
date_default_timezone_set($local_timezone);
$local = date("Y-m-d h:i:s A");

date_default_timezone_set("GMT");
$gmt = date("Y-m-d h:i:s A");

$require_timezone = $timezoneRequired;
date_default_timezone_set($require_timezone);
$required = date("Y-m-d h:i:s A");

date_default_timezone_set($system_timezone);

$diff1 = (strtotime($gmt) - strtotime($local));
$diff2 = (strtotime($required) - strtotime($gmt));

$date = new DateTime($time);
$date->modify("+$diff1 seconds");
$date->modify("+$diff2 seconds");
$timestamp = $date->format("Y-m-d h:i:s A");
return $timestamp;
}
[/sourcecode]
[sourcecode language="php"]
echo ConvertGMTToLocalTimezone("2013-03-16 14:28:00","Asia/Seoul")."
";
echo ConvertLocalTimezoneToGMT("2013-03-16 14:28:00","Asia/Seoul")."
";
echo ConvertOneTimezoneToAnotherTimezone("2013-03-16 14:28:00","Asia/Seoul","Australia/South");
[/sourcecode]

Thursday, March 7, 2013

Python Email Crawler Class

[caption id="attachment_775" align="alignnone" width="202"]Email Crawler Email Crawler[/caption]

[sourcecode language="python"]
import os
import re

class EmailCrawler:
email_list = []
emailregex = re.compile('\w+[@][a-zA-Z_\.]+\.[a-zA-Z]{2,6}')
output_path = os.getcwd()

def setBaseURL(self,url):
self.tocrawl = set([url])

def run(self):
dir = raw_input('Enter the directory path for crawled files\n')
self.verifyDir(dir)
self.crawl(dir)

def output(self):
with open("emails.txt", "w") as a:
for email in self.email_list:
a.write(str(email) + os.linesep)
print email

def verifyDir(self,path):
if not os.path.exists(path):
print "This directory does not exist"
exit

def crawl(self,dir_path):
print "Crawling Email links in " + dir_path + "....\n\n"
for path, subdirs, files in os.walk(self.get_raw_string(dir_path)):
for filename in files:
filePath = os.path.join(path, filename)
f=open(filePath, 'r')
html=f.read()
f.close()
results = self.emailregex.findall(html)
if results:
for email in results:
if email not in self.email_list:
self.email_list.append(email)
self.output()

def get_raw_string(self,text):
"""Returns a raw string representation of text"""
escape_dict={'\a':r'\a',
'\b':r'\b',
'\c':r'\c',
'\f':r'\f',
'\n':r'\n',
'\r':r'\r',
'\t':r'\t',
'\v':r'\v',
'\'':r'\'',
'\"':r'\"'}

new_string=''
for char in text:
try:
new_string += escape_dict[char]
except KeyError:
new_string += char
return new_string

[/sourcecode]

How to run this email crawler?
Load and run this program in Python Shell. (In Python shell, Run >> Run Module)
import email_crawler
e = email_crawler.EmailCrawler()
e.run()

Python Web Crawler Class

[caption id="attachment_775" align="alignnone" width="202"]Web Crawler web crawler[/caption]

[sourcecode language="python"]
import sys
import re
import urllib2
import urlparse
import datetime
import os

class WebCrawler:
tocrawl = set([])
crawled = set([])
linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')

def setBaseURL(self,url):
self.tocrawl = set([url])

def run(self):
url = raw_input('Enter an URL to crawl\n')
self.setBaseURL(url)
dir = raw_input('Where should I put crawled HTML source files?\n')
self.crawl(dir)

def getTitle(self,html):
startPos = html.find('<title>')
if startPos != -1:
endPos = html.find('</title>', startPos+7)
if endPos != -1:
title = html[startPos+7:endPos]
return title

def writeToFile(self,url):
with open('hyperlinks.txt', 'a') as file:
file.write(url + '\n')

def writeHTML(self,fileName,html,dirPath):
self.verifyDir(dirPath)
fileName = re.sub('[\\/:"*?<>|]',"",fileName)
with open(dirPath+'\\'+ fileName + '.txt', 'w+') as file:
file.write(html)

def verifyDir(self,path):
if not os.path.exists(path):
os.makedirs(path)

def crawl(self,dir_path):
while 1:
try:
if self.tocrawl:
crawling = self.tocrawl.pop()
print '\n\nStart Crawling - ' + crawling + '\n'
except KeyError:
raise StopIteration
url = urlparse.urlparse(crawling)
try:
response = urllib2.urlopen(crawling)
except:
continue
msg = response.read()
self.writeHTML(crawling,msg,dir_path)

#Display page title
print self.getTitle(msg)

links = self.linkregex.findall(msg)
self.crawled.add(crawling)
self.writeToFile(crawling)
for link in links:
if link.startswith('mailto'):
continue
if link.startswith('/'):
link = 'http://' + url[1] + link
elif link.startswith('#'):
link = 'http://' + url[1] + url[2] + link
elif not link.startswith('http'):
link = 'http://' + url[1] + '/' + link
if link not in self.crawled:
print '----' + link
self.tocrawl.add(link)

[/sourcecode]

How to run this crawler?

Run following command in Python shell.


import web_crawler
w = web_crawler.WebCrawler()
w.run()

Monday, March 4, 2013

Dealing with Time Zones in PHP/MySQL

[caption id="attachment_768" align="alignnone" width="502"]timezone timezone[/caption]

You might have experienced numerous problems when dealing with timezones. It is difficult to handle timezones when your web server and users are in different timezones. The condition become worse if the database server is in another timezone.

Let's see a simple way of storing date,time in database without messing up timezone related things. The datetime can be stored in GMT format without explicitly using a  specific timezone.

[sourcecode language="php"]
$gmtTime = gmdate("Y-m-d H:i:s", time());
[/sourcecode]

the timestamp value returned by time() function is stored in GMT format. This is neutral and can be stored in database without thinking timezone problems.

When querying data and make any date time comparisons you can convert it into a desired timezone using mysql built-in function CONVERT_TZ().

Following query can be used to retrive records added on a particular today with the consideration of a specific timezone.
This is based on 'America/Denver' timezone  ( -7:00).

[sourcecode language="php"]
SELECT *
FROM `audittrail`
WHERE date( CONVERT_TZ( datetime, '+0:00', '-7:00' ) ) = '2013-03-04 '

[/sourcecode]


How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...