Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Tuesday, December 8, 2015

OPENCV on Python on my WIndows 8.1 tablet

I have just installed OPENCV on Python on my WIndows 8.1 tablet and I have been playing with it like crazy. Opencv is a powerful tool to programmatically manipulate an image. It is like a low level version of Photoshop. And what happens when a programmer gets to programmaticaly manipulate an image? The possibilities are endless.

I had skipped the "hello world" part because I am not new to opencv and here is what I have tried to develop as part of my learning: finding the exact location of a small image taken from a larger image.

This ois the large image:

And obviously this is the small piece of image:


 And here is the result:

 
 
And here is the python code I used:
# import the necessary packages
import numpy as np
import cv2

# load the big_image  and small_image  images
big_image = cv2.imread('C:\\Users\\mybook\\Documents\\alpha.jpg')
small_image = cv2.imread('C:\\Users\\mybook\\Documents\\piece.jpg')
(small_imageHeight, small_imageWidth) = small_image.shape[:2]

# find the small_image in the big_image
result = cv2.matchTemplate(big_image, small_image, cv2.TM_CCOEFF)
(_, _, minLoc, maxLoc) = cv2.minMaxLoc(result)

# grab the bounding box of small_image  and extract it from
# the big image
topLeft = maxLoc
botRight = (topLeft[0] + small_imageWidth, topLeft[1] + small_imageHeight)
roi = big_image[topLeft[1]:botRight[1], topLeft[0]:botRight[0]]

# construct a darkened transparent 'layer' to darken everything
# in the big_image except for small_image
mask = np.zeros(big_image.shape, dtype = "uint8")
big_image = cv2.addWeighted(big_image, 0.25, mask, 0.75, 0)

# put the original small_image  back in the image so that it is
# 'brighter' than the rest of the image
big_image[topLeft[1]:botRight[1], topLeft[0]:botRight[0]] = roi

# display the images
cv2.imshow("Big_Image", big_image)
cv2.imshow("Small_Image", small_image)
cv2.waitKey(0)
This may a simple app but it can be useful for game development, or it even be further developed and improved to create an image search engine.

Wednesday, April 29, 2015

Configuring the Ibeacon to Detect other BLE(Bluetooth 4.0) Devices with Raspberry Pi and Python

The Ibeacon from Ziruz Technologies is not just simply an Ibeacon, it can also be configured to detect other Bluetooth Low Energy(BLE) devices. And more recently, one AT command has been added to make the Ibeacon detect other Ibeacons, but the HM10 module will need a firmware upgrade so that the new command will be available for use.

We can use the previous setup we used in my previous article(Configuring Ibeacons using Raspberry Pi B+ and Python) so that all we need to do is create a python program. And here is the python program I used to scan other BLE devices:
import serial
import time
port = serial.Serial("/dev/ttyAMA0", baudrate=9600, timeout=3.0)
port.write("AT+RENEW")
rcv = port.readline() 
print rcv
rcv = ""
port.write("AT+RESET")
rcv = port.readline() 
print rcv
rcv = ""
port.write("AT+ROLE1")
rcv = port.readline() 
print rcv
rcv = ""
port.write("AT+IMME1")
rcv = port.readline() 
print rcv
rcv = ""
port.write("AT+SHOW1")
rcv = port.readline() 
print rcv
rcv = ""
while True:
    port.write("AT+DISC")
    rcv = port.readline() 
    print rcv
    rcv = ""
    time.sleep(10)

The AT+DISC will scan all BLE devices and will just return the MAC address, the newest firmware version has the command AT+DISI? and this command will return the UUID, RSSI, Major, Minor, Measured Power and the MAC Address. The command AT+DISI? is available with firmware version V539, to check the version of the firmware use AT+VERS?

If your current Ibeacon does not support the latest version, you can update the firmware by following this procedure.