<commit_before>import roslib
roslib.update_path('opencv_util')
from image_msgs.msg import Image as RosImage
from std_msgs.msg import UInt8MultiArray
from std_msgs.msg import MultiArrayLayout
from std_msgs.msg import MultiArrayDimension

import opencv.adaptors as ad
import Image as pil
import numpy as np

##
# Convert from ROS to OpenCV image datatype
# @param image to convert
def ros2cv(image):
    #ros to pil then pil to opencv
    if image.encoding != 'bgr' and image.encoding != 'rgb':
        raise RuntimeError('Unsupported format "%s"' % image.encoding)
    if image.depth != 'uint8':
        raise RuntimeError('Unsupported depth "%s"' % image.depth)

    height    = image.uint8_data.layout.dim[0].size
    width     = image.uint8_data.layout.dim[1].size
    channels  = image.uint8_data.layout.dim[2].size
    np_image  = np.reshape(np.fromstring(image.uint8_data.data, dtype='uint8', count=height*width*channels), [height, width, channels])
    return ad.NumPy2Ipl(np_image)

##
# Simple drawing of text on an image with a drop shadow
def text(image, x, y, a_string):
    font = cvInitFont(CV_FONT_HERSHEY_SIMPLEX, .3, .3)
    cvPutText(image, a_string, cvPoint(x, y),     font, cvScalar(0,0,0))
    cvPutText(image, a_string, cvPoint(x+1, y+1), font, cvScalar(255,255,255))

##
# Tries to clean up segmentation 
# @param cv_image
# @param n_times to run erode & dilate
# @return image in original type with hopefully cleaner segmentations
def clean_segmentation(cv_image, n_times=1):
    dst  = cv.cvCloneImage(cv_image)
    dst2 = cv.cvCloneImage(cv_image)
    cv.cvErode(cv_image, dst, None, n_times)
    cv_image = cv.cvDilate(dst, dst2, None, n_times)
    return dst2

##
# Mask a color image with a given black and white mask
# @param img
# @param img_mask one channeled image
# @return color image with masked part filled with black
def mask(img, img_mask):
    dim      = img.width, img.height
    depth    = img.depth
    channels = img.nChannels

    r_chan = cvCreateImage(cvSize(*dim), depth, 1)
    g_chan = cvCreateImage(cvSize(*dim), depth, 1)
    b_chan = cvCreateImage(cvSize(*dim), depth, 1)
    combined = cvCreateImage(cvSize(*dim), depth, 3)
    cvSplit(img, r_chan, g_chan, b_chan, None)

    cvAnd(r_chan, img_mask, r_chan)
    cvAnd(g_chan, img_mask, g_chan)
    cvAnd(b_chan, img_mask, b_chan)
    cvMerge(r_chan, g_chan, b_chan, None, combined)
    return combined

##
# Split a color image into its component parts
def split(image):
    img1 = cvCloneImage(image)
    img2 = cvCloneImage(image)
    img3 = cvCloneImage(image)
    cvSplit(image, img1, img2, img3, None)
    return (img1, img2, img3)
###
## Fill holes in a binary image using scipy
##
#def fill_holes(cv_img):
#    img_np     = ut.cv2np(cv_img)
#    img_binary = img_np[:,:,0]
#    results    = ni.binary_fill_holes(img_binary)<commit_msg>added functions scavenged from dog_commands project     text - draw text with drop shadow so that they're easy to see     clean_segmentation - given a binary image, clean it with errodes & dilates     mask - mask a color image given a binary mask     split - split up a color image into its components     scale - returns scaled version of images<commit_after>import roslib
roslib.update_path('opencv_util')
from image_msgs.msg import Image as RosImage
from std_msgs.msg import UInt8MultiArray
from std_msgs.msg import MultiArrayLayout
from std_msgs.msg import MultiArrayDimension

import opencv as cv
import opencv.highgui as hg
import opencv.adaptors as ad
import Image as pil
import numpy as np

##
# Convert from ROS to OpenCV image datatype
# @param image to convert
def ros2cv(image):
    #ros to pil then pil to opencv
    if image.encoding != 'bgr' and image.encoding != 'rgb':
        raise RuntimeError('Unsupported format "%s"' % image.encoding)
    if image.depth != 'uint8':
        raise RuntimeError('Unsupported depth "%s"' % image.depth)

    height    = image.uint8_data.layout.dim[0].size
    width     = image.uint8_data.layout.dim[1].size
    channels  = image.uint8_data.layout.dim[2].size
    np_image  = np.reshape(np.fromstring(image.uint8_data.data, dtype='uint8', count=height*width*channels), [height, width, channels])
    return ad.NumPy2Ipl(np_image)

##
# Simple drawing of text on an image with a drop shadow
def text(image, x, y, a_string):
    font = cv.cvInitFont(CV_FONT_HERSHEY_SIMPLEX, .3, .3)
    cv.cvPutText(image, a_string, cv.cvPoint(x, y),     font, cv.cvScalar(0,0,0))
    cv.cvPutText(image, a_string, cv.cvPoint(x+1, y+1), font, cv.cvScalar(255,255,255))

##
# Tries to clean up segmentation 
# @param cv_image
# @param n_times to run erode & dilate
# @return image in original type with hopefully cleaner segmentations
def clean_segmentation(cv_image, n_times=1):
    dst  = cv.cvCloneImage(cv_image)
    dst2 = cv.cvCloneImage(cv_image)
    cv.cvErode(cv_image, dst, None, n_times)
    cv_image = cv.cvDilate(dst, dst2, None, n_times)
    return dst2

##
# Mask a color image with a given black and white mask
# @param img
# @param img_mask one channeled image
# @return color image with masked part filled with black
def mask(img, img_mask):
    dim      = img.width, img.height
    depth    = img.depth
    channels = img.nChannels

    r_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1)
    g_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1)
    b_chan = cv.cvCreateImage(cv.cvSize(*dim), depth, 1)
    combined = cv.cvCreateImage(cv.cvSize(*dim), depth, 3)
    cv.cvSplit(img, r_chan, g_chan, b_chan, None)

    cv.cvAnd(r_chan, img_mask, r_chan)
    cv.cvAnd(g_chan, img_mask, g_chan)
    cv.cvAnd(b_chan, img_mask, b_chan)
    cv.cvMerge(r_chan, g_chan, b_chan, None, combined)
    return combined

##
# Split a color image into its component parts
def split(image):
    img1 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1)
    img2 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1)
    img3 = cv.cvCreateImage(cv.cvSize(image.width, image.height), 8, 1)
    cv.cvSplit(image, img1, img2, img3, None)
    return (img1, img2, img3)

###
## Fill holes in a binary image using scipy
##
#def fill_holes(cv_img):
#    img_np     = ut.cv2np(cv_img)
#    img_binary = img_np[:,:,0]
#    results    = ni.binary_fill_holes(img_binary)
{'fraction_non_alphanumeric': 0.10945989133908597, 'fraction_numerical': 0.01597954618088846, 'mean_word_length': 3.963521015067407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540512', 'n_tokens_mistral': 2252, 'n_tokens_neox': 2198, 'n_words': 615}