How to get image width and height in OpenCV? [duplicate] - python

This question already has answers here:
Size of Matrix OpenCV
(5 answers)
Closed 6 years ago.
I want to get image width and height, how can I do that in OpenCV?
For example:
Mat src = imread("path_to_image");
cout << src.width;
Is that right?

You can use rows and cols:
cout << "Width : " << src.cols << endl;
cout << "Height: " << src.rows << endl;
or size():
cout << "Width : " << src.size().width << endl;
cout << "Height: " << src.size().height << endl;
or size
cout << "Width : " << src.size[1] << endl;
cout << "Height: " << src.size[0] << endl;

Also for openCV in python you can do:
img = cv2.imread('myImage.jpg')
height, width, channels = img.shape

Related

Write access violation occurring in C++ program

I am trying to do a homework assignment that reads an integer in from C++ and passes it to a Python function that will display a multiplication table or double the integer. Note: the callIntFunc() function was given code by my instructor. I only wrote the code in main():
int callIntFunc(string proc, int param)
{
char* procname = new char[proc.length() + 1];
std::strcpy(procname, proc.c_str());
PyObject* pName, * pModule, * pDict, * pFunc, * pValue = nullptr, * presult = nullptr;
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyUnicode_FromString((char*)"PythonCode");
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, procname);
if (PyCallable_Check(pFunc))
{
pValue = Py_BuildValue("(i)", param);
PyErr_Print();
presult = PyObject_CallObject(pFunc, pValue);
PyErr_Print();
}
else
{
PyErr_Print();
}
//printf("Result is %d\n", _PyLong_AsInt(presult));
Py_DECREF(pValue);
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
// clean
delete[] procname;
return _PyLong_AsInt(presult);
}
void displayMenu() {
cout << setfill('=') << setw(45) << " " << endl;
cout << "1: Display a Multiplication Table" << endl;
cout << "2: Double a Value" << endl;
cout << "3: Exit" << endl;
cout << "Enter your selection as number 1, 2, or 3" << endl;
cout << setfill('=') << setw(45) << " " << endl;
}
int main()
{
int userInput;
displayMenu();
cin >> userInput;
//While loop for input validation to check to see if anything other than 1, 2 or 3 is entered
while (userInput != 1 && userInput != 2 && userInput != 3) {
cout << "Invalid input, enter 1, 2 or 3" << endl;
displayMenu();
cin >> userInput;
}
//Creates a multiplication table with given number if user entered 1
if (userInput == 1) {
cout << "Enter a number to create a multiplication table" << endl;
cin >> userInput;
cout << callIntFunc("MultiplicationTable", userInput) << endl;
}
//Doubles the given number if user entered 2
else if (userInput == 2) {
cout << "Enter a number to double it" << endl;
cin >> userInput;
cout << callIntFunc("DoubleValue", userInput) << endl;
}
//Exits program if user entered 3
else if (userInput == 3) {
cout << "Exiting..." << endl;
exit(0);
}
cout << "\nDone\n";
return 0;
}
The error occurs on the line with Py_DECREF(pValue). I also posted the call stack:
> Integrating Languages.exe!callIntFunc(std::string proc, int param) Line 129 C++
Integrating Languages.exe!main() Line 190 C++
[External Code]
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
import re
import string
def printsomething():
print("Hello from python!")
def PrintMe(v):
print("You sent me: " + v)
return 100;
def SquareValue(v):
return v * v
def MultiplicationTable(num):
for i in range(1, 11):
print(str(num) + "x " + str(i) + "= " + str(num * i))
return -1
def DoubleValue(num):
return num * 2

Convert typedef struct from C++ to Python

I'm trying to port the following series of videos from C++ to Python.
You can find them in YT: https://www.youtube.com/channel/UCzGKCXSeHjDRSmpX9SmQ1fw/videos
The library with the 12000 words can be found here: https://pastebin.com/E5sDrFJ5
The idea behing is to build a solver for crosswords from scratch. So far, up to module 9, my Python code is equivalent to the C++ version:
def ToUpper(s):
return s.upper()
class Library:
def __init__(self):
self.words = []
self.counts = {}
def IsWord(self, s):
for w in self.words:
if s == w:
return True
return False
def GetWord(self, i):
assert (i >= 0 and i < len(self.words))
return self.words[i]
def ReadFromFile(self, filename):
with open(filename, 'r') as f:
for line in f:
# print(f"{line.rstrip()} ({len(line.rstrip())})")
line = ToUpper(line)
self.words.append(line.rstrip())
print(f"Read {len(self.words)} words from file '{filename}'")
def ComputeStats(self):
assert self.counts == {}
for i in range(18):
self.counts[i] = []
for s in self.words:
_len = len(s)
if _len < 18:
self.counts[_len].append(_len)
def PrintStats(self):
print("Here are the counts of each word length")
for k,v in self.counts.items():
if k != 0:
print(f"[{k}] {len(v)}")
class Grid:
def __init__(self, n):
self.name = n
self.lines = []
def rows(self):
return len(self.lines)
def cols(self):
if self.lines == []:
return 0
else:
return len(self.lines[0])
def LoadFromFile(self, filename):
with open(filename, 'r') as f:
for line in f:
# print(f"{line.rstrip()} ({len(line.rstrip())})")
if not line.startswith('#'):
self.lines.append(line.rstrip())
def Check(self):
for s in self.lines:
assert len(s) == self.cols()
def Print(self):
print(f"Grid: {self.name} "
f"(rows={self.rows()},"
f" cols={self.cols()})")
for s in self.lines:
print(f" {s.rstrip()}")
if __name__ == "__main__":
lib = Library()
lib.ReadFromFile("top_12000.txt")
lib.ComputeStats()
lib.PrintStats()
print(lib.IsWord("DOG"))
print(lib.IsWord("FUFFJFJ"))
print(lib.IsWord("THANKS"))
# grid = Grid("MY GRID")
# grid.LoadFromFile('test')
# grid.Check()
# grid.Print()
The C++ version from the video is this one:
#include <iostream>
#include <string>
#include <vector>
#include <assert.h>
#include <fstream>
using namespace std;
string ToUpper(string s) {
string s2;
for (char c : s) {
s2.push_back(toupper(c));
}
return s2;
}
class Library {
public:
bool IsWord(string s) const {
for (string t : words) {
if (s == t) {
return true;
}
}
return false;
}
void ComputeStats() {
assert(counts.empty());
counts.resize(18);
for (string s : words) {
int len = s.length();
if (len < 18) {
counts[len]++;
}
}
}
void PrintStats() const {
cout << "Here are the counts of each word length:\n";
for (int i=1; i<counts.size(); i++) {
cout << "[" << i << "] " << counts[i] << "\n";
}
}
string GetWord(int i) const {
assert(i >= 0 && i < words.size());
return words[i];
}
void ReadFromFile(string filename) {
ifstream f;
f.open(filename);
while (!f.eof()) {
string line;
getline(f, line);
// cout << line << " (" << line.length() << ")\n";
if (!line.empty()) {
line = ToUpper(line);
int len = line.length();
if (line[len-1] == '\r') {
line = line.substr(0, len-1);
}
words.push_back(line);
}
}
cout << "Read " << words.size() << " words from file '"
<< filename << "'\n";
}
private:
vector<string> words;
vector<int> counts;
};
struct Grid {
Grid(string n) {
name = n;
}
int rows() const { return lines.size(); }
int cols() const {
if (lines.empty()) {
return 0;
} else {
return lines[0].size();
}
}
void LoadFromFile(string filename) {
ifstream f;
f.open(filename);
while (!f.eof()) {
string line;
getline(f, line);
cout << line << " (" << line.length() << ")\n";
if (!line.empty() && line[0] != '#') {
lines.push_back(line);
}
}
}
void Check() const {
for (int i=0; i<lines.size(); i++) {
assert(lines[i].size() == cols());
}
}
void Print() const {
cout << "Grid: " << name
<< " (rows=" << rows()
<< ", cols=" << cols() << ")\n";
for (string s : lines) {
cout << " " << s << "\n";
}
}
string name;
vector<string> lines;
};
int main() {
Library lib;
lib.ReadFromFile("top_12000.txt");
lib.ComputeStats();
lib.PrintStats();
cout << lib.IsWord("DOG") << "\n";
cout << lib.IsWord("FUFFJFJ") << "\n";
cout << lib.IsWord("THANKS") << "\n";
// Grid grid("MY GRID");
// grid.LoadFromFile("test");
// grid.Check();
// grid.Print();
But I got stuck in lesson 9 because there is no "struct" data type in Python. In the video for lesson 9, this new structure "Word" was introduced. I highligthed the changes:
**struct Word {
Word(string s) {
word = s;
}
string word;
};
typedef vector<Word> Words;**
class Library {
public:
bool IsWord(string s) const {
for (**Word w** : words) {
if (s == **w.word**) {
return true;
}
}
return false;
}
void ComputeStats() {
assert(counts.empty());
counts.resize(18);
for (**Word w** : words) {
int len = **w.word**.length();
if (len < 18) {
counts[len]++;
}
}
}
void PrintStats() const {
cout << "Here are the counts of each word length:\n";
for (int i=1; i<counts.size(); i++) {
cout << "[" << i << "] " << counts[i] << "\n";
}
}
string GetWord(int i) const {
assert(i >= 0 && i < words.size());
return words[i]**.word**;
}
void ReadFromFile(string filename) {
ifstream f;
f.open(filename);
while (!f.eof()) {
string line;
getline(f, line);
// cout << line << " (" << line.length() << ")\n";
if (!line.empty()) {
line = ToUpper(line);
int len = line.length();
if (line[len-1] == '\r') {
line = line.substr(0, len-1);
}
words.push_back(**Word(line)**);
}
}
cout << "Read " << words.size() << " words from file '"
<< filename << "'\n";
}
private:
**Words words**;
vector<int> counts;
}
How should I proceed? Is it just to create a class "Word" like this?
class Word:
def __init__(self, n):
self.word = n
I'm missing the typedef, because there are not such structures in Python. And then, how to use this new class in the "Library" class?
class Library:
def __init__(self):
self.words = Words()
self.counts = {}
Because this is giving me a TypeError:
TypeError: __init__() missing 1 required positional argument: 'n'
Any help will be appreciated.

Own array-implementation in Python

I was able to create my own class in C++ which is basically an array of fixed size, where I can append, retrieve, and change integers in.
This is the C++-code:
#include <iostream>
class List {
public:
//Item-object needed to store
//elements
struct Item {
int data = 0;
Item* next = nullptr;
};
//Constructor
//creates a list of fixed size
List(int size) {
//Filling up with empty items
for (int i = 0; i < size; i++) {
Item* item = new Item;
//If first item,
//store start-address
//set buffer to start address
if (i == 0)
this->start = item;
//Setting current item as nextptr
//for previous item in buffer
if (i > 0)
this->buffer->next = item;
//Storing current address in buffer
this->buffer = item;
//Outputting address and value (just for testing purposes)
//std::cout << "Address: " << &item << " -> " << item->data << std::endl;
}
}
//Destructor
~List() {
Item* current = this->start;
while(current) {
delete current;
current = current->next;
}
}
//Returns address of very first element in list
Item* getFirstItemAddress() {
return this->start;
}
//Append to list
bool append(int value) {
Item* start = this->start;
//Calculating insertIndex
int insertIndex = this->index *2;
//Inserting
(start + insertIndex)->data = value;
//Increment index for next call
this->index++;
return true;
}
//Retrieve item at index from list
int retrieve(int i) {
//Error check (index out of range)
if (i >= this->index) {
std::cerr << "Index out of range!" << std::endl;
throw("Index out of range");
return -1;
}
i = i * 2;
return (start + i)->data;
}
//Remove element at index from list
bool remove(int index) {
index = index * 2;
//Shifting all elements above 'index'
//one down
Item* current = (this->start + index);
while (current->next) {
Item* next = current->next;
current->data = next->data;
current = next;
}
//Decrementing index
this->index--;
return true;
}
//Change element to value at index
bool change(int index, int value) {
index = index * 2;
(this->start + index)->data = value;
return true;
}
//Printing out list
void print() {
int counter = 0;
Item* current = this->getFirstItemAddress();
while (current) {
if (counter >= this->index) break;
std::cout << current->data << std::endl;
current = current->next;
counter++;
}
}
private:
//Holding address of first item
Item* start = nullptr;
//Buffer holding temporary address
Item* buffer = nullptr;
//Variable pointing to indexToInsert
int index = 0;
};
int main() {
List list(5);
//Printing out
/*std::cout << std::endl << "Testing output:" << std::endl;
List::Item* current = list.getFirstItemAddress();
while (current) {
std::cout << current->data << std::endl;
current = current->next;
}*/
List::Item* start = list.getFirstItemAddress();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.append(5);
std::cout << list.retrieve(0) << std::endl;
std::cout << list.retrieve(1) << std::endl;
std::cout << list.retrieve(2) << std::endl;
std::cout << list.retrieve(3) << std::endl;
std::cout << list.retrieve(4) << std::endl;
list.remove(2);
std::cout << std::endl;
list.print();
std::cout << std::endl;
list.change(3, 8);
std::cout << list.retrieve(3) << std::endl;
return 0;
}
But I actually want it in Python 3,
Python Code:
class Item:
data = None
next = None
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class List:
start = Item()
buffer = Item()
index = 0
def __init__(self, size):
for i in range(0, size):
item = Item
if (i == 0):
start = item
if (i > 0):
self.buffer.next = item
buffer = item
def getFirstItemAddress(self):
return self.start
def append(self, value):
start = self.start
insertIndex = self.index * 2
(addressof(start) + insertIndex).data = value
self.index+=1
return True
def retrieve(self, i):
if i >= self.index:
print("Index out of range")
return -1
exit()
i = i * 2
return (self.start + i).data
def remove(self, index):
index = index * 2
current = (self.start + index)
while current.next:
next = current.next
current.data = next.data
current = next
self.index-=1
return True
def print(self):
counter = 0
current = self.getFirstItemAddress()
while current:
if counter >= self.index: break
print(current.data)
current = current.next
counter+=1
list = List(5)
list.append(1)
That code ain't working a little bit. I get errors like:
Traceback (most recent call last):
File "main.py", line 73, in <module>
list.append(1)
File "main.py", line 34, in append
(start + insertIndex).data = value
TypeError: unsupported operand type(s) for +: 'Item' and 'int'
Which is pretty logic, however, I need to be able to use SOME KIND of pointers and references in Python. Because I have to calculate index'es based on start-addresses etc.
I have researched a bit and stumbled on functions like 'id()', and 'addressof()' but those weren't working.
Without using special implementations like PyPy or CPython, how would I achieve this? I knew that in Python you have very little options to do memory management, but it's said that everything done in C++ can be done in Python... So how would I create my own made array-list implementation in Python?
TL;DR
I have created my own 'array-implementation' in C++, but I want it in Python3. However Python has very little memory management options like 'new', pointers, references,... I'm still searching for a way to achieve this in Python.

Convert Matrix Multiplication from Python to C++

My first programming language is python and now im trying to convert the python code to C++ code. C++ code will be used later in QT Creator.
My python code is :
import numpy as np
P0 = 10
P1 = 20
P2 = 30
P3 = 40
Koef = [[-1,3,-3,1],[2,-5,4,-1],[-1,0,1,0],[0,2,0,0]]
mKoef = np.matrix(Koef)
Px = [[P0],[P1],[P2],[P3]]
mPx = np.matrix(Px)
t = 0.01
sr = [t**3,t**2,t,1]
msr = np.matrix(sr)
C = 0.5 * msr * mKoef * mPx
print(C)
And the result which i get is 20.1. But if I try to do same operation in C++ i get '0x75cc60' with no errors. I don't even know what it means.
My C++ code (UPDATED!) :
Now i get correct result 20.1. But for some reason if i comment out MatrixP2 the code doesn't work. And there seems to be a problem with reading values from MatrixProduct for some reason.
float t = 0.01;
int P0 = 10;
int P1 = 20;
int P2 = 30;
int P3 = 40;
float t3 = pow(t,3);
float t2 = pow(t,2);
int MatrixKoef[4][4] = {{-1, 3, -3, 1}, {2, -5, 4, -1}, {-1, 0, 1, 0}, {0, 2, 0, 0}};
float MatrixSR[1][4] = {t3,t2,t,1};
int MatrixP[4][1] = {{P0},{P1},{P2},{P3}};
int MatrixP2[1][4] = {5,4,3,2};
float MatrixProduct[1][1] = {0};
float MatrixFinale[1] = {0};
for (int row = 0; row < 1; row++) {
for (int col = 0; col < 4; col++) {
for (int inner = 0; inner < 4; inner++) {
MatrixProduct[row][col] += 0.5 * MatrixSR[row][inner] * MatrixKoef[inner][col];
}
std::cout << MatrixProduct[row][col] << " ";
}
std::cout << "\n";
}
for (int row = 0; row<1;row++){
for (int col = 0; col<4; col++){
MatrixFinale[0] += MatrixProduct[1][-1+col] * MatrixP[0][col];
std::cout << MatrixProduct[1][-1+col] << " ";
std::cout << MatrixP[0][col] << " ";
std::cout << col;
std::cout << "\n";
}
std::cout << MatrixFinale[0] << " ";
}
All help will be appreciated! Thank you.
I am not sure why you are looping, but it should be
std::cout << MatrixFinale[row] << " ";
Also, as everybody has already pointed out, '0x75cc60' is the memory address of you MatrixFinale variable.
After fixing matrix declaration. All problems were fixed.
Correct C++ code :
float t = 0.01;
int P0 = 10;
int P1 = 20;
int P2 = 30;
int P3 = 40;
float t3 = pow(t,3);
float t2 = pow(t,2);
int MatrixKoef[4][4] = {{-1, 3, -3, 1},
{2, -5, 4, -1},
{-1, 0, 1, 0},
{0, 2, 0, 0}};
float MatrixSR[] = {t3,t2,t,1};
int MatrixP[] = {P0,P1,P2,P3};
float MatrixProduct[] = {0,0,0,0};
float MatrixFinal[] = {0};
for (int row = 0; row < 1; row++) {
for (int col = 0; col < 4; col++) {
for (int inner = 0; inner < 4; inner++) {
MatrixProduct[col] += 0.5 * (MatrixSR[inner] * MatrixKoef[inner][col]);
}
}
for (int inner = 0; inner < 4; inner++) {
MatrixFinal[0] += MatrixProduct[inner] * MatrixP[inner];
}
std::cout << MatrixFinal[0] << " ";
}

Print blob names in Pycaffe

I can print blob names in C++ as follow.
for (int index = 0; index < net_->blobs().size(); ++index) //net_->layer_names().size()
{
const boost::shared_ptr<Blob<float> > blob = net_->blob_by_name(net_->blob_names()[index]);
int batch_size = blob->num();
int dim_features = blob->count() / batch_size;
std::cout << "Blob name:" << "'" <<net_->blob_names()[index] << "'" << " batch size " << "'" << batch_size << "'" << " channels:" << "'" << blob->channels() << "'" << " height:" << "'" << blob->height() << "'" << " width:" << "'" << blob->width() << "'" <<std::endl;
}
I am looking for similar api in python.
I know only to print layer names in python, how to print blob names and size?
This is for printing layer names
for layer_name, blob in net.blobs.iteritems():
print layer_name + '\t' + str(blob.data.shape)

Categories

Resources