Make Image Blue Pytorch - python

I'm trying to "blueify" an image that I will use for a PyTorch application (AI), which works better with "bluer" images. Specifically, I want each pixel to be blueish. I will put the code inside a class which I will put in a transforms.Compose for and pass it to the torchvision.datasets. ImageFolder tranform key word argument.
I tried to use the PyTorch torchvision.transforms.functional functions (adjust_hue, adjust_saturation, adjust_brightness). However, I was always getting images with different colors (e.g. green & purple, red & blue). I will put them inside a class which I will put in a transforms.Compose for and pass it to the torchvision.datasets.ImageFolder tranform key word argument.
Can you please help?

Related

When using Augmentations in Detectron2 are Bounding Boxes transformed automatically?

I'm currently trying to implement Augmentations in Detectron2 and my code is looking like this:
class AugmentationTrainer(DefaultTrainer):
#classmethod
def build_train_loader(cls, cfg):
mapper = DatasetMapper(cfg, is_train=True,augmentations=[
T.RandomBrightness(0.5,1.5),
T.RandomContrast(2, 2),
T.RandomSaturation(2 ,2),
T.RandomFlip(prob=1, horizontal=True, vertical=False),
T.RandomFlip(prob=0.5, horizontal=False, vertical=True),
])
return build_detection_train_loader(cfg, mapper = mapper)
I'm simply using this class as a Trainer instead of the Default one. I was wondering if the bounding box data gets changed properly using this, when using Augmentations like Flip or Rotate for example.
Thanks in Advance!
I trained with the above augmentations, but my end result was worse than the training without augmentations, which confused me a lot.

Setting Custom RoI for OpenCV BoundryBox in Python

I'm trying to implement Object Tracker using OpenCV and I'm new to Python. I'll call it from C# code via IronPython. What I'm trying to do, I want to set a custom rectangle as a parameter to Tracker instead of selecting it by mouse.
(Tracker code is the common example you can find on the internet)
Here is the problematic part :
This is how I set and create a rectangle
initBB = cv2.rectangle(frame ,(154, 278),(173,183), (0, 255, 00),1)
This is Tracker's init method
tracker.init(frame, initBB)
and this is the error
SystemError: new style getargs format but argument is not a tuple
If I wanted to use "normal" way, initBB set would be like
initBB = cv2.selectROI("Frame", frame, fromCenter=False,
showCrosshair=False)
I couldn't see which part I'm doing wrong, am I trying to set the wrong type of object to initBB or setting it in wrong way?
Thanks! Have a nice day!
Your error comes from a misunderstanding of what cv2.rectangle does.
It doesn't return a rectangle as you imagine. It is actually a drawing function. It draws the rectangle on the image you pass as argument and returns None.
A rectangle is just a tuple in Python with the following coordinates: (start_col, start_row, width, height). You can create it without using an OpenCV function.

How to detect moving object in varying light(illumination) conditions due to clouds - openCV

I have been trying to detect moving vehicles. But due to varying light conditions because of clouds, (not shadows of clouds, just illuminations) the background subtraction fails.
I have uploaded my input video here --> Youtube (30secs)
Here is what I got using various available background subtraction methods available in opencv
import numpy as np
import cv2
cap = cv2.VideoCapture('traffic_finalns.mp4')
#fgbgKNN = cv2.createBackgroundSubtractorKNN()
fgbgMOG = cv2.bgsegm.createBackgroundSubtractorMOG(100,5,0.7,0)
#fgbgGMG = cv2.bgsegm.createBackgroundSubtractorGMG()
#fgbgMOG2 = cv2.createBackgroundSubtractorMOG2()
#fgbgCNT = cv2.bgsegm.createBackgroundSubtractorCNT(15,True,15*60,True)
while(1):
ret, frame = cap.read()
# fgmaskKNN = fgbgKNN.apply(frame)
fgmaskMOG = fgbgMOG.apply(frame)
# fgmaskGMG = fgbgGMG.apply(frame)
# fgmaskMOG2 = fgbgMOG2.apply(frame)
# fgmaskCNT = fgbgCNT.apply(frame)
#
# cv2.imshow('frame',frame)
# cv2.imshow('fgmaskKNN',fgmaskKNN)
cv2.imshow('fgmaskMOG',fgmaskMOG)
# cv2.imshow('fgmaskGMG',fgmaskGMG)
# cv2.imshow('fgmaskMOG2',fgmaskMOG2)
# cv2.imshow('fgmaskCNT',fgmaskCNT)
k = cv2.waitKey(20) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
(Below images -> Frame number - 977)
BackgroundSubtractorMOG : By varying the input parameter history some illumination could be reduced, but not all, as the duration of illumination is variable
BackgroundSubtractorMOG2 :
BackgroundSubtractorGMG :
**BackgroundSubtractorKNN : **
BackgroundSubtractorCNT
1] Improving results by OpenCV Background Subtraction
For varying light conditions it is important to normalize your pixal values between 0 and 1. In your code I do not see that happening
Background subtraction will not work with a single image (In your code you are reading an image)
If you are applying background subtraction on sequence of frames then the first frame of background subtraction result is of no use
you might want to adjust the arguments of the cv2.bgsegm.createBackgroundSubtractorMOG() that you are passing to get the best results... Play around with the threshold and see what results do you get
You can also apply gaussian filter to the individual frames to reduce noise and get better results cv2.GaussianBlur()
You can try cv2.equalizeHist() on individual frame so that you improve the contrast of the frames
Anyways you say that you are trying to detect moving object. Nowadays there are many modern methods that use deep-learning for object detection
2] Use tensorflow object detection api
It does object detection in real-time and also gives you the bounding box co-ordinate of the detected objects
Here are results of Tensorflow object detection api:
3] How about trying Opencv Optical Flow
4] Simple subtraction
Your environment is static
So take a frame of your environment and store it in a variable say environment_frame
Now read every frame from your video and simply subtract it from your environment frame results = environment_frame - current_frame
Now if the np.sum(results) is greater than a threshold value then we say there is a object
Now if np.sum(results) is greater then threshold then we know there is a moving object but where ???
The moving object is where there are clustered cluttered pixels which you can easily find by some clustering algorithm
Do not forget to normalize your pixel values between 0 and 1
----------------------------UPDATED----------------------------------------
If you want to find helmets in real time then your best bet is deep-learning
You can use a deep learning technique like YOLO which newer version of OpenCV has ... but I do no think they have a python binding for YOLO in OpencV
The other real time technique can be RCNN which the tensorflow object detection api already has .... I have mentioned it above
If you want to use traditional computer vision methods then you can try hog and svm for helmet data and then you can try a sliding window technique to find the helmet in your frame (This won't be in real time)

How to make transparent background white instead of black when converting PDF to JPG using PythonMagick

I'm trying to convert from PDF to JPG using PythonMagick, but I can't find a way to set the background color, which by default is changed from transparent to black. I can get the desired result using os.system and the -flatten parameter as shown below.
import os
os.system('convert -flatten -background \#ffffff -density 400 -adaptive-resize 1900x infile.pdf outfile.jpg')
However, PythonMagick does not seem to have a flatten method and the following snippet produces an image with a black background.
import PythonMagick
import os
img = PythonMagick.Image("infile.pdf")
img.backgroundColor('#ffffff')
img.density('400')
img.resize('1900x')
img.magick('JPG')
img.quality(60)
img.write("outfile.jpg")
There is also a transparent() method that takes a color. I'm not quite sure what it's for, but img.transparent('#ffffff') did not help. Is there another way to achieve the same result? I'd rather not do it using os.system, since it seems to take quite alot longer.
If you look at the documentation for the -flatten command-line option, you will see it is an alias for -layers flatten.
The -layers flatten command is itself a combination command, which comprises creating a layer of the current background colour the size of the first images canvas, and then composing each layer in turn on top of it.
PythonMagick is essentially just a binding layer to the Magick++ C++ interface. The advanced commands that convert provides, are not necessarily replicated in the lower level libraries, as they are really a sequence of commands as described above. So whilst there is no single command for it in the PythonMagick library, the functionality can be replicated.
The method you are after is .composite(), the PythonMagick documentation is so limited ( or indeed non-existent), most people stay clear of the library. But I think the usage is something like this, if there was only one layer in the PDF (totally untested):
import PythonMagick
img = PythonMagick.Image("infile.pdf")
img.density('400')
bgColour = PythonMagick.ColorRGB(1.0, 1.0, 1.0)
size = "%sx%s" % (img.columns(), img.rows())
flattened = PythonMagick.Image(size, bgColour)
flattened.type = img.type
flattened.composite(img, 0, 0, PythonMagick.CompositeOperator.SrcOverCompositeOp)
flattened.resize('1900x')
flattened.magick('JPG')
flattened.quality(60)
flattened.write("outfile.jpg")
NB. The composition operator could be PythonMagick.CompositeOperator.DstOverCompositeOp, I'm not sure which way round it is handling that.
Though PDFs are a special case with ImageMagick, as they are usually passed off to ghostscript to rasterize. Which means you might need to give ghostscript (gs) some odd parameters to handle the alpha channel properly. Try adding verbose options to the command that works to see what delegate commands it issues and consider doing the PDF rasterisation yourself via an os.system('gs ...') command and then doing the resize. Though I doubt that would be faster than just calling convert.

applying image decoration (border) in Python (programmatically)

I am looking for a way to create a border in python.Is there any library in Python which we can import to create a border.
Note that I do not want to use any image masks to create this effect (e.g. I don't want to use any image editing package like GIMP to create a border image mask) .
Here is what I am looking for:
import fooImageBorders
import Image
foo = Image.open("someImage.jpg")
foo2 = fooImageBorders.bevel(foo, color = black)
...I can write my own methods to add borders .. but if there is already something like this out there with a comprehensive set of border options, I would like to make use of it.
I looked at PIL documentation and couldn't find a way to do this. I have windows xp and there doesn't seem to be a way to install PythonMagick either for Python 2.6 if you don't have cygwin.
Look at the ImageOps module within the PIL.
import Image
import ImageOps
x = Image.open('test.png')
y = ImageOps.expand(x,border=5,fill='red')
y.save('test2.png')
You can use the PythonMagick module. the documentation for this module is here (Magic ++ documentation)
Example: To add a red 2 pixel border to an image, you need following code.
from PythonMagick import Image
i = Image('example.jpg') # reades image and creates an image instance
i.borderColor("#ff0000") # sets border paint color to red
i.border("2x2") # paints a 2 pixel border
i.write("out.jpg")
# writes the image to a file
foo2 = foo.copy()
draw = ImageDraw.Draw(foo2)
for i in range(width):
draw.rectangle([i, i, foo2.size[0]-i-1, foo2.size[1]-i-1], outline = color)
foo2 will have a width-pixel border of color.
If you want different colored borders on each side, you can replace .rectangle with repeated .line calls.
If you want the border not to cover any part of the existing image, use this instead of foo.copy().
foo2 = Image.new(foo.mode, (foo.size[0] + 2*width, foo.size[1] + 2*width))
foo2.paste(foo, (width, width))

Categories

Resources